Login/Logout system not working (Been dealing with this for a while)

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 596 Views - Last Post: 18 March 2014 - 03:08 PM Rate Topic: -----

#1 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Login/Logout system not working (Been dealing with this for a while)

Posted 09 March 2014 - 06:25 PM

I'm about tired of this.

Basically, when I'm testing the script. I get to login. Then I log out. But for some odd reason, when I visit either the home page or the login page again, it displays the "Logout" button which is supposed to mean that I am logged in.
I have no idea why.

head.php:
<?php session_start();
require 'func/usermanage.php';?>
<html><title>Web!</title><style>body{text-align:center;background-color:#4609ed;}</style><body><img src="http://website.com/images/sv_logo.png"><br>
<table border="0" align="center"><tr><th><?php if(LoggedIn()===false){ echo '<a href="http://website.com/login.php"><img src="/images/sv_login.png"></a></th>'; } else { echo '<th><a href="http://www.website.com/logout.php"><img src="images/sv_logout.png"></a></th>'; }?><th><a href="http://website.com/index.php"><img src="/images/home.png"></a><a href="http://www.website.com/browse.php"><img src="/images/sv_browse.png"></a><a href="http://www.website.com/account.php"><img src="/images/sv_account.png"></a></th></tr></table>
</body></html>


logout.php
<?php
include 'inc/head.php';
unset($_SESSION['svuser']);
unset($_SESSION['login']);
$_SESSION['login']=0;
$_SESSION['svuser']="";
session_destroy();
echo "<b>You have been logged out, thanks for visiting!</b>";
?>


func/usermanage.php:


function LoggedIn()
{
if(isset($_SESSION['login']) && $_SESSION['login']===1)
{
return true;
} else {return false;}}


login.php:
<?php 
if(LoggedIn()===false)
{
if(isset($_POST['submit']))
{
if(ValidCredentials($_POST['User'],sha1(md5($_POST['Pass'])))===true)
{
$_SESSION['svuser']=$_POST['User'];
$_SESSION['login']=1;
echo "<br><b>You have been logged in!</b>";
} else { echo "<br><b><font color='red'>Invalid username or password!</font></b>";}} else {echo "<br><b><font color='red'>You are already logged in!</font></b>";}}


I'd really appreciate if someone can help me!

Is This A Good Question/Topic? 0
  • +

Replies To: Login/Logout system not working (Been dealing with this for a while)

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 736
  • View blog
  • Posts: 3,220
  • Joined: 03-December 12

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 09 March 2014 - 11:25 PM

Check the value returned from LoggedIn(). It probably is not returning the way you are thinking.
Was This Post Helpful? 0
  • +
  • -

#3 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 10 March 2014 - 05:19 PM

The return value of LoggedIn() on login.php is always 1 (true).
There is no return value on the logout.php page.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 736
  • View blog
  • Posts: 3,220
  • Joined: 03-December 12

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 10 March 2014 - 05:33 PM

What is it on head.Php?
Was This Post Helpful? 0
  • +
  • -

#5 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 10 March 2014 - 06:18 PM

Its still the same.
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 736
  • View blog
  • Posts: 3,220
  • Joined: 03-December 12

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 10 March 2014 - 10:22 PM

So 'regardless of you being logged in, it returns 1?

This post has been edited by astonecipher: 10 March 2014 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 449
  • View blog
  • Posts: 1,629
  • Joined: 15-January 14

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 11 March 2014 - 09:45 AM

If that function returns true, then $_SESSION['login'] is set to 1. That might be because you're not using session_start on the logout page, or maybe the pages are cached. You can use print_r to print the session to see what's there:

print_r($_SESSION);
Was This Post Helpful? 0
  • +
  • -

#8 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 11 March 2014 - 05:20 PM

From print_r($_SESSION):

Array ( [svuser] => Administrator [login] => 1 ) 


What I have observed is:
Everytime I go to a page other than the home-page, the $_SESSION array is empty
Everytime I go to the home-page, the $_SESSION array is valid. (Even if I just logged out).
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 449
  • View blog
  • Posts: 1,629
  • Joined: 15-January 14

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 12 March 2014 - 09:58 AM

Those symptoms sound like the problem is that you're not using session_start on every page. You need to use session_start on every page where you use the session, regardless of whether you're logging in, logging out, or just checking the session data. If you don't use session_start then the $_SESSION array will not be populated and any changes you make to the session will not persist.
Was This Post Helpful? 0
  • +
  • -

#10 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 12 March 2014 - 03:48 PM

I added session_start () to the header which is included on every page though
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 449
  • View blog
  • Posts: 1,629
  • Joined: 15-January 14

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 12 March 2014 - 04:04 PM

Before session_start, add this to make sure you're seeing errors if they happen:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Was This Post Helpful? 0
  • +
  • -

#12 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 12 March 2014 - 06:30 PM

I showed all the errors and they are no errors D:
Was This Post Helpful? 0
  • +
  • -

#13 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 449
  • View blog
  • Posts: 1,629
  • Joined: 15-January 14

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 12 March 2014 - 06:35 PM

I don't really see a reason why it would behave like that, I feel like we're not seeing some of the code. Although your logout code is a little redundant.

<?php
include 'inc/head.php';
unset($_SESSION['svuser']);
unset($_SESSION['login']);
$_SESSION['login']=0;
$_SESSION['svuser']="";
session_destroy();
echo "<b>You have been logged out, thanks for visiting!</b>";
?>



You unset the session variables, then set them again to something else, then use session_destroy to remove them. You can just use session_destroy.

You can also use this code to remove the session cookie, from the manual for session_destroy:

if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}


Like session_start though, you can only use setcookie before you have sent output. Trying to use it after will have no effect, it will be an error.

This post has been edited by ArtificialSoldier: 12 March 2014 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#14 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 13 March 2014 - 04:39 PM

I changed up the logout.php code but that still doesn't work.
I think the main issue is the fact that the other pages aren't seeing the cookies or wahtever, right now. :S
But again, I have no idea whats going on.
Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 449
  • View blog
  • Posts: 1,629
  • Joined: 15-January 14

Re: Login/Logout system not working (Been dealing with this for a while)

Posted 13 March 2014 - 04:48 PM

You can verify that with your browser's developer tools. You can use the Net tab to see the requests going out, each request should also show you the cookies sent with that request. Unless you changed it from the default, the session cookie will be called PHPSESSID.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2