By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

Setting $_SESSION in PHP 5

P: 4
I am having problems getting the $_SESSION to set, the $_SESSION will work fine when its first gets the value, however, when I go to another page, the $_SESSION then loses its value. So if any one can take a look at my code and see if there is any problems, that would be good.

Thanks

Here is my code

[PHP]
<?php if (!isset($_SESSION))
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_POST['txtUserName']))
{
mysql_select_db($database_con, $con);
$LoginRS = mysql_query("SELECT * FROM User WHERE passwd='".$_POST['txtPassword']."' AND login='".$_POST['txtUserName']."'" , $con or die(mysql_error());
$row_loginFoundUser = mysql_fetch_assoc($LoginRS);
$loginFoundUser = mysql_num_rows($LoginRS);

if ($loginFoundUser > 0)
{
//declare three session variables and assign them
$_SESSION['conUserID'] = $row_loginFoundUser['UserID'];
$_SESSION['UserEMail'] = $row_loginFoundUser['Email'];
$_SESSION['UserName'] = $row_loginFoundUser['FName']." ".$row_loginFoundUser['LName'];
}//end of if
}//end of if ?>


<form action="<?php echo $loginFormAction; ?>" method="post" name="frmLogin" id="frmLogin">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left">
<br/><label>My Business Continuity <br />Plans</label>
</td>
</tr>

<?php //checks if the user log in
if (isset($_SESSION['conUserID']))
{
echo "<tr>
<td align=\"left\">
<a href=\"Page2.php?UserID=".$_SESSION['conUserID']."\">Go To Page 2</a>
<br/><br/>
</td>
</tr>
</td></tr>";
}//end of if
else
{
echo "<tr>
<td>
<label>User Name:</label>
</td>
</tr>
<tr>
<td>
<input name=\"txtUserName\" type=\"text\" id=\"txtUserName\" />
</td>
</tr>
<tr>
<td>
<label>Password:</label>
</td>
</tr>
<tr>
<td>
<input name=\"txtPassword\" type=\"password\" id=\"txtPassword\" />
</td>
</tr>";
}//end of else?>
</table>[/PHP]
Jan 2 '08 #1
Share this Question
Share on Google+
8 Replies


P: 52
I think the problem is here


[PHP]
<?php if (!isset($_SESSION))
session_start();
[/PHP]

You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

How about trying just
[PHP]session_start();[/PHP]

It would start a session in every page.
I think that would help
Jan 3 '08 #2

P: 4
I have try that suggestion however it is still not working

I think the problem is here

You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

How about trying just
[PHP]session_start();[/PHP]

It would start a session in every page.
I think that would help
Jan 3 '08 #3

P: 4
I am also using PHP version 5.2.4 if that makes any differance
Jan 3 '08 #4

P: 6
Hi,

I m using PHP Version 5.2.5 and it is working for, and this is what i did in every page i include

[PHP]session_start();

if ($_SESSION['username'] == '')
{
include "loginerror.php";
die;
exit;
break;
}
$username = $_SESSION['username'];[/PHP]

My login.php has this

[PHP]$username = $_POST[username];

$username = strtolower($username);
session_start();

$_SESSION['username'] = $username;[/PHP]

Hope this helps

Regards,
Sumeet
Jan 3 '08 #5

P: 52
Have you included
session_start();

in another page or not?

You need to start a session in another page as well.
Jan 4 '08 #6

P: 11
You can leave the isset() criteria out, just start every page with

Expand|Select|Wrap|Line Numbers
  1.  session_start(); 
You have to make sure that you that there is no character or space or anything outputted before you use the session_start() function (this will output an error also, about output before the headers have been send).

Also make sure that your browser accepts cookies (sessions are actually cookies until you close your browser). I know that Opera has a nice function to browse cookies.
Jan 7 '08 #7

P: 4
Sorry, for the late reply.

I have found the problem, the server for some reason keeps changing the session ids on each page on the site.
Jan 25 '08 #8

Atli
Expert 5K+
P: 5,058
Hi.

The most likely reason for the server refreshing the ID is that it cannot find your previous session, or that your browser is unable to store the session ID.

Make sure that the user that is running PHP has write privileges on the directory used to store the session data. You can find the location of that directory in your php.ini under "session.save_path". Also make sure the "session.use_cookies" directive is set to On.
If your server is using Windows, the "session.save_path" directive must be set.
Jan 25 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.