Need Help with Sessions Please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Need Help with Sessions Please

Post by jdhorton77 »

Ok, I've got my session variables working for the most part. Like if I log in it stores the values of the customer id and such in session variables and goes to another page where it displays the value of some of the variables. But I'm having trouble when I go to refresh a page for some reason it looses the values, like the session was destroyed. Anybody have an idea on what's going on?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Do you have

Code: Select all

session_start();
on the top of EVERY page?
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Sorry for my delayed responce. But yes, I have session_start() on top of every page. It only drops the value when I hit the refresh button. I don't know if I said earlier, but what I'm doing is I have a session variable named "loggedIn" that I'm using like a boolean, which is either 1 for true or 0 for false. So when the person logs in on the Home page it sets this value to 1. Then when they click on a link to go to a products page it holds its value and still shows "Sign in as: Joe Shmoe". But when you refresh the page it refreshes showing that the user is not signed in. Meaning the "Sign In" link is displayed.

I was thinking of creating a javascript function for the reload event and try embedding php in it. But if this can be corrected other wise I'm all ears.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Post the source code on the page is failing, that might help.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Ok, here's the source code for the login page. It's not only happening with this page, it seems to do it with every page when you refresh it. Hope this helps.

Code: Select all

<?php
session_start();
?>

<html>
<head>
<title>Customer Sign-In</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript" type="text/javascript">
window.personalbar = false
window.scrollbars = false
window.statusbar = false
window.toolbar = false
window.outerwidth="650"
window.outerheight="500"
</script>

<?php
     $connection = mysql_connect("localhost","admin","nunya");
     mysql_select_db("weprint", $connection);
     $error = 0;

?>
</head>

<body>

<table align="center" width="800">
<tr>
<td>

<?php  include("menu.php");
	
if (array_key_exists('submitted', $_POST))
{
     $username = $_POST["username"];
     $password = $_POST["password"];
	
     $result = mysql_query("select * from uname where username = '$username'");
     if (mysql_result($result,0,0) == $username && mysql_result($result,0,1) == $password)
     {
          $num = mysql_result($result,0,2);
          $info = mysql_query("select * from customers where customerid = '$num'");
          $_SESSION['fname'] = mysql_result($info,0,1);
          $_SESSION['mi'] = mysql_result($info,0,2);
          $_SESSION['lname'] = mysql_result($info,0,3);
          $_SESSION['street'] = mysql_result($info,0,4);
          if (mysql_result($info,0,5) != "")   {$_SESSION['apt'] = mysql_result($info,0,5);}
          $_SESSION['city'] = mysql_result($info,0,6);
          $_SESSION['state'] = mysql_result($info,0,7);
          $_SESSION['zip'] = mysql_result($info,0,8);
          $_SESSION['eaddress'] = mysql_result($info,0,9);
          $_SESSION['areacode'] = mysql_result($info,0,10);
          $_SESSION['prenum'] = mysql_result($info,0,11);
          $_SESSION['postnum'] = mysql_result($info,0,12);
          $_SESSION['loggedIn'] = 1;                         // <----  here is where logged in gets set
		
     else {$error = 1;}
?>
this is supposed to be the HTML form code, hope I pressed the right button. Apologies if I didn't...
Everah | there is PHP code in it, so you can use PHP tags, which I have done for you.

Code: Select all

<form action="<?php $_SERVER[PHP_SELF]; ?>" method="post">
<table width="200" align="left">
     
     <?php 
     if ($error == 1 && array_key_exists('submitted', $_POST))
     { ?>
          
     <tr>
          <td colspan="2" width="200" align="left"><p style="color:red">Username or Password Incorrect</p></td>
     </tr>	<?php } ?>
		
     <tr>
          <td width="100" align="left"><p>UserName:</p></td>
          <td width="100" align="left">
               <input type="text" name="username" id="username" size="12" maxlength="12">
          </td>	
     </tr>
		
     <tr>
          <td width="100" align="left"><p>Password:</p></td>
          <td width="100" align="left">
               <input  type="password" name="password" id="password" size="12" maxlength="12">
          </td>	
     </tr>
		
     <tr>	
          <td colspan="2" width="200"><br>
               <a href="http://localhost/weprint4you/customerinformation.php" 
                 style="font-size:75%; color: #aa1100">Register an account</a><br>
				
               <a href="http://localhost/weprint4you/forgotten.php" 
                 style="font-size:75%; color: #aa1100">Forgot your user information</a><br><br>
					
               <input type="submit" name="submit" value="Login">
               <input type="hidden" name="submitted" value="submitted">
          </td>
     </tr>	
</table>
</form>

</td>
</tr>
</table>			

</body>
</html>
Preciate all that you do to help me out.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Where is the part where you check against $_SESSION['loggedIn']?
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Sorry, I posted the loginWindow which just sets the value to 1. Here's a product page which will check for loggedIn status.

Code: Select all

<?php
session_start();

?>

<html>
<head>
<title>Product Selection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
a {font-family:nina; font-size: 90%}
a:link {color: #666666; text-decoration:none}
a:visited {color: #666666; text-decoration:none}
a:hover {color: #FF0000; text-decoration:underline}
a:active {color: #FF0000}
p {font-family: nina; font-size: 100%}
#welcome {font-family: nina; font-size: 115%}

</style>

<?php
     function signOut()
     {
          session_destroy();
          echo 'http://localhost/weprint4you/weprintMain.php';
     }
	
?>

</head>

<body>
<?php 

include("menu.php"); ?>

<table width="800" align="center">
     <tr><td colspan="2">&nbsp;</td></tr>
	
     <tr>
          <td align="left" valign="middle">
          <span id="nav"><a href="http://localhost/weprint4you/weprintMain.php">Home</a> -- Products Selection</span>
          </td>
          
          <?php if($_SESSION['loggedIn'] == 1){ ?>     //<---- loggedIn checked here
          <td align="right" valign="middle">
               <span><p id="sia">Signed in as:<?php echo ' '.$_SESSION['fname'].' '.$_SESSION['lname'];?></p></span>
          </td>
     </tr>
     <tr>
          <td align="right" valign="middle" colspan="2">
               <span><a href="<?php signOut();?>">Sign Out</a></span>
               <span><a href="http://localhost/weprint4you/customerAccount.php">Customer Account</a></span>
          </td>
          
          <?php } else { ?>
          <td align="right" valign="middle">
               <span><a href="LoginWindow.php">Sign In</a></span>
          </td>
          <?php } ?>
     </tr>
</table>


</body>
</html>
I have marked above where loggedIn is checked.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

In the code you just posted, before the session check but after the session start, add in this and tell us what comes of it:

Code: Select all

<?php
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Ok, I did what you said and it printed the values of the session variables on the page. Was that what it was supposed to do? And then when I pressed the refresh button it showed that all values were gone?

Here is how the code looks with that.

Code: Select all

<?php
session_start();

?>

<html>
<head>
<title>Product Selection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
a {font-family:nina; font-size: 90%}
a:link {color: #666666; text-decoration:none}
a:visited {color: #666666; text-decoration:none}
a:hover {color: #FF0000; text-decoration:underline}
a:active {color: #FF0000}
p {font-family: nina; font-size: 100%}
#welcome {font-family: nina; font-size: 115%}

</style>

<?php
     function signOut()
     {
          session_destroy();
          echo 'http://localhost/weprint4you/weprintMain.php';
     }
	
?>

</head>

<body>

<?php 
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>

<?php include("menu.php"); ?>

<table width="800" align="center">
     <tr><td colspan="2">&nbsp;</td></tr>
     <tr>
          <td align="left" valign="middle">
          <span id="nav"><a href="http://localhost/weprint4you/weprintMain.php">Home</a> -- Products Selection</span>
          </td>

     <?php if($_SESSION['loggedIn'] == 1){ ?>
          <td align="right" valign="middle">
          <span><p id="sia">Signed in as:<?php echo ' '.$_SESSION['fname'].' '.$_SESSION['lname'];?></p></span>
          </td>
     </tr>

     <tr>
          <td align="right" valign="middle" colspan="2">
          <span><a href="<?php signOut();?>">Sign Out</a></span>
          <span><a href="http://localhost/weprint4you/customerAccount.php">Customer Account</a></span>
          </td>
     <?php } else { ?>
          <td align="right" valign="middle">
          <span><a href="LoginWindow.php">Sign In</a></span>
          </td>
     <?php } ?>
     </tr>
</table>


</body>
</html>
I was wondering, are you supposed to set the values of the variables on every page? Or are they supposed to hold the values until they are destroyed?
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

The session variables stay until they are destroyed or outlived their timeframe.

Example:

Code: Select all

$_SESSION['loggedIn'] = true; // this will stay active the WHOLE session no matter what page
However:

Code: Select all

$someString = true; // this will only stay true for ONE execution; NOT the whole session and NOT every page.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your telling your session to end in your code.

Code: Select all

<span><a href="<?php signOut();?>">Sign Out</a></span>
Your function does this:

Code: Select all

function signOut()
{
    session_destroy();
    echo 'http://localhost/weprint4you/weprintMain.php';
}
That means that when you call that function in the href tag your are in effect killing your session.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

yeah, but that's only done when the sign out link is clicked on. Or is it? This problem occurs on the home page where the user signs in also and there's no session_destroy() there. I don't know what's going on here.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

jdhorton77 wrote:yeah, but that's only done when the sign out link is clicked on. Or is it? This problem occurs on the home page where the user signs in also and there's no session_destroy() there. I don't know what's going on here.
No, its not done when the link is clicked, anything between <?php and ?> is executed every single time the php script loads no matter where it is in the HTML.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

jdhorton77 wrote:yeah, but that's only done when the sign out link is clicked on. Or is it? This problem occurs on the home page where the user signs in also and there's no session_destroy() there. I don't know what's going on here.
When you call this function, you are telling PHP to:

Code: Select all

<?php
function signOut()
{
    // Destroy the session
    session_destroy();

    // Echo out some text
    echo 'http://localhost/weprint4you/weprintMain.php';
}
?>
So no, your code is not click dependent. The code in the function will do what you have told it to as soon as you call the function.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Well I put that code snippet after the signout function at the bottom of the page and even after the code goes by that function call it holds the values of the session variables.

So how should a sign out function be written if this is the problem?
Post Reply