Jump to content

Recommended Posts

I have created a login script and it seems to work great - most of the time??

 

Sometimes (and I stress. . .sometimes), it logs the user out when they go from page to page. Here is the code I use to check it user is logged in:

<?php
function session_defaults() {
	$_SESSION['logged'] = false;
	$_SESSION['uid'] = 0;
	$_SESSION['username'] = '';
	$_SESSION['cookie'] = 0;
	$_SESSION['remember'] = false; 
}

session_start();
if(!isset($_SESSION['logged']) ) {
	session_defaults();
}

if($_SESSION['logged'] == true){
	echo('<span class="login"><a href=' . $root . 'login/logout.php>Logout</a>');
}
else{
	echo('<span class="login"><a href="#" onClick="$(\'#ifxElement\').BlindDown(500);return false;">Login</a>');
}

include("login/index.php");
?>

 

Is there an obvious reason why this won't work? I end the db check with this to set $_SESSION['logged'] prior to the code shown above:

 

if($row){
	session_start();
	$_SESSION['logged'] = true;
	header("Location: http://www.tablashirt.com"); 
}
else{
	header("Location: http://www.tablashirt.com/login/login_failure.php"); 
}

 

Thanks

RyanOD

Link to comment
https://forums.phpfreaks.com/topic/50880-help-staying-logged-in/
Share on other sites

Oh, oops. Well, that happened when I was screwing around with the code. In reality, that mistake isn't there.

 

The issue is this. I have one page that passes a product id through the use of the $_GET global variable. The page that receives that variable logs me out. Is there some sort of conflict between $_SESSION and $_GET? The weird thing is. . .it only happens once in a while!! Ugggghhhhh.

 

Thanks for the help.

 

RyanOD

Ok, here is some more craziness. . .

 

I have the following in my masthead.php file. It is included in every page.

 

if(isset($_SESSION['logged']) && ($_SESSION['logged'] == true)){
echo('<span class="login"><a href=' . $root . 'login/logout.php>Logout</a></span>');
}
else{
echo('<span class="login"><a href="#" onclick="$(\'#ifxElement\').BlindDown(500);return false;">Login</a></span>');
}

 

I start the session prior to my <doctype> tag in the head.php file (this too is included in every page).

 

The logout.php file that is called is the following:

 

<?php
session_start();
session_unset();
session_destroy();

header( "Location: http://www.tablashirt.com" );
?>

 

Now, when I click on 'Logout' from anywhere except the product page, it logs out and sends me to the main page. Fantastic! Oddly enough, when I do the same thing from a product page, it send me to the main page, but it doesn't log me out. How can that be?? The code is identical in both cases.

 

Even more weird is the fact that the reverse holds true as well!! If I'm on the main page and I log in, it works fine, but when I click to the product page, I get logged off.

 

Thanks once again. . .RyanOD

I would begin by looking at the product page since it is the page giving you fits. Try to determine what is different about that page in terms of how you handle the session. There has to be *some* kind of difference on that page.

 

I don't see how a $_GET and $_SESSION could interfere with each other as they are 2 separate beasts in the SUPERGLOBAL world. I may be wrong here, but I don't see it happening.

 

Post the code for the product page if the above does not help at all and we'll try to figure it out.

 

Regards, Nate

Oh man, I am just going absolutely crazy with this. I don't see how there could be any issue. . .however, there is. I apologize for all the code I am posting, but I am at my wits end. . .

 

Here is the index.html file for products:

<?php
$path = "/home/tablashi/public_html/";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
$root="../";
include("db/db_connect.php");
include("db/db_select.php");
include("head/head.php");
include("products/body.php");
include("db/db_close.php");
?>

 

Here is the index file for the main page:

<?php
$path = "/home/tablashi/public_html/";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
$root="";
include("db/db_connect.php");
include("db/db_select.php");
include("head/head.php");
include("home/body.php");
        include("db/db_close.php");
?>

 

Here is the products/body.php page:

<body>
<?php
	include("home/masthead.php");
	include("products/maincontent.php");
	include("home/subcontent.php");
	include("footer/footer.php");
?>
</body>
</html>

 

Here is the home/body.php page:

<body>
<?php
	include("home/masthead.php");
	include("home/maincontent.php");
	include("home/subcontent.php");
	include("footer/footer.php");
?>
</body>
</html>

 

Here is the home/maincontent.php:

<div id="wrap">
<img src="<?php echo"$root" . "images/headers/header_fd.gif"?>" alt="Featured Designs" />
<div id="main">
	<?php			
		$sql = "SELECT * FROM product WHERE product_featured = 1";
		$result = mysql_query($sql) or die('<p>Unable to perform product query. Error: ' . mysql_error() . '</p>');

		while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo('<div class="product_mini"><a href="../products?prod_id=' . $row[prod_id] . '"><img class="product_thumb" src="' . $row[product_image] . '" /></a></div>');
		}
	?>
</div>

 

And here is the products/maincontent.php:

<div id="wrap">
<div id="main">
	<?php	
		$prod_id = $_GET['prod_id'];	
		$sql = "SELECT * FROM product WHERE prod_id = $prod_id";
		$result = mysql_query($sql) or die('<p>Unable to perform product query. Error: ' . mysql_error() . '</p>');

		while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo('<h3 id="prod_title">' . $row[product_name] . ' </h3>');
			echo('<div class="product"><img src="' . $root . $row[product_image] . '" /></div>');
			echo('<p id="prod_description">' . $row[product_description] . ' </p>');
		}
	?>
</div>

 

Here is the masthead.php file that is shared where the login/logout is located.

<div id="masthead">
<a href="<?php echo("$root")?>"><img src="<?php echo("$root" . "images/logo.gif")?>" alt="Tablashirt" /></a>
<?php
	include("session/session_default.php");

	if(!isset($_SESSION['logged']) ) {
		session_defaults();
	}

	if(isset($_SESSION['logged']) && ($_SESSION['logged'] == true)){
		echo('<span class="login"><a href=' . $root . 'login/logout.php>Logout</a></span>');
	}
	else{
		echo('<span class="login"><a href="#" onclick="$(\'#ifxElement\').BlindDown(500);return false;">Login</a></span>');
	}

	include("login/index.php");
?>
<div id="nav">
	<ul>
		<li><a href="<?php echo"$root" . "submit/"?>">Submit</a></li>
		<li><a href="<?php echo"$root" . "score/"?>">Score</a></li>
		<li><a href="<?php echo"$root" . "shop/"?>">Shop</a></li>
		<li><a href="<?php echo"$root" . "registration/"?>">Join</a></li>
	</ul>
</div>
</div>

 

And finally, here is the logout.php page:

<?php
session_start();
session_unset();
session_destroy();
header( "Location: http://www.tablashirt.com" );
?>

 

10,000,000 gold stars to anyone who can solve this issue.

 

RyanOD

Ok, that is good to know. Apparently, there are no problems in those files. If I put my session_start(); in the head file, does it matter where it goes?? I wanted to make sure it came before ANY content was displayed so I put it before the <doctype> declaration.

 

Is there some reason why putting it there would cause problems. Also, if I am redirecting from the logout.php file with header:, does that cause any conflict with the actual <head>?? I think not, but I'm desperate!!

 

Thanks again!!

 

RyanOD

Can anyone tell me why sometimes my link to my products page produces the following:

 

http://tablashirt.com/products/?prod_id=3&PHPSESSID=590cbb53e690cdd5729b79f42fd71cb5

 

and other times it is the following:

 

http://tablashirt.com/products/?prod_id=3

 

I know the obvious answer might be that in the first case a session has been started. However, every page on this site has the same head.php file included and session_start(); is the first thing in the head.php file.

 

As is the case, there should always be a valid session and every page on the site should have access to the $_SESSION variable. Why then is it passed in the URL only sometimes? Moreover, I have one page that has no access to the $_SESSION variable. All the pages use the exact same include files yet one page doesn't work - all the others do. I actually went through the pain of duplicating the working page in the directory that contains the file that doesn't work and when it is in that directory, it doesn't work either. Weird.

 

Are there directory permission settings that need to be changed. I'm starting to think my problem is a server issue. . .not a PHP issue - or is that just wishful thinking?

 

Thanks.

 

RyanOD

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.