PHP session variables not working correctly

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

Post Reply
lervy
Forum Newbie
Posts: 4
Joined: Tue Mar 10, 2009 6:43 am

PHP session variables not working correctly

Post by lervy »

I have 2 pages one which checks the sql database for existence of a username and password and then creates a session variable and then one page which checks for the existence of this variable before proceeding.

I am new to PHP so I would appreciate your patience but have been using asp/asp.net for about 10 years so I do have a good grasp of concepts.

This is the code which checks the username and password and tries to create the session variable:

Code: Select all

<?
  include "connections.php";
  include "queries.php";
  
  $dbcnx2;
  
  $info = mysql_fetch_array( $login );
  $rows = mysql_num_rows($login);
 
  if ($rows == 0)
        header("Location: login.php");
  else
        {
        session_start();
        $_session['uname'] = $info['uname'];
        header("Location: admin.php");
        }
?>
I have put an echo directly after this line ($_session['name'] = $info['uname'];) to verify that the variable is being created properly, and below is the code which I want to verify if the session variable has been created successfully. But at the moment it redirects back to the login screen as if the variable is blank, I put in echo $_session['uname'] after session_start(); to see if anything is output but as expected this is blank. Any help is greatly appreciated.

Code: Select all

<?
session_start();
if($_session['uname'] == "")
header("Location: login.php");
else
echo "Welcome " . $_session['uname'];
?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: PHP session variables not working correctly

Post by JeffG »

Variables in PHP are case-sensitive. You have (I think - others will correct me if I'm wrong) created an array $_session with a single entry. You need to use $_SESSION.
lervy
Forum Newbie
Posts: 4
Joined: Tue Mar 10, 2009 6:43 am

Re: PHP session variables not working correctly

Post by lervy »

Hi thanks for the reply, I still get the same problem unfortunately, I am wondering if there is something on the server maybe which is preventing the sessions from working. According to my provider I am using PHP 4.4.7 if this helps. I have made the changes to the code and it now looks like this:

File 1:

Code: Select all

<?
  include "connections.php";
  include "queries.php";
  
  $dbcnx2;
  
  $info = mysql_fetch_array( $login );
  $rows = mysql_num_rows($login);
    
  if ($rows == 0)
        header("Location: login.php");
  else
        {
        session_start();
        $_SESSION['uname'] = $info['uname'];
        header("Location: admin.php");
        }
?>
File 2:

Code: Select all

<?
session_start();
if($_SESSION['uname'] == "")
header("Location: login.php");
else
echo "Welcome " . $_SESSION['uname'];
 
?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>
The Challenger
Forum Newbie
Posts: 3
Joined: Tue Mar 10, 2009 10:17 am

Re: PHP session variables not working correctly

Post by The Challenger »

session_start();
if($_session['uname'] == "")
header("Location: login.php");
else
echo "Welcome " . $_session['uname'];
?>
The problem is you're using the sentece worng
you have to do the if read the session is NULL not "" you can use this sentece

if(empty($_SESSION['uname'])){
header("LOcation: login.php");
}
else
{
echo "welcome" $_SESSION['uname'];
}

Sorry my english i'm form portugal and have a long time didn't pratice my english
lervy
Forum Newbie
Posts: 4
Joined: Tue Mar 10, 2009 6:43 am

Re: PHP session variables not working correctly

Post by lervy »

Hi, Your english was great thanks, unfortunately this has still not resolved the issue. If on file 2 I put in:

Code: Select all

<?
session_start();
echo $_SESSION['uname']
?>
 
instead of the redirect 'if' statement I just get a blank page instead of it saying my username.

My code for file 2 now looks like this:

Code: Select all

 
<?
session_start();
 
if(empty($_SESSION['uname']))
{
header("Location: login.php");
}
else
{
echo "Welcome" . $_SESSION['uname'];
}
 
?>
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: PHP session variables not working correctly

Post by JeffG »

It worked for me on my local server when I replaced the (deprecated) introducer '<?' with '<?php', and not before.

This may be your problem.
lervy
Forum Newbie
Posts: 4
Joined: Tue Mar 10, 2009 6:43 am

Re: PHP session variables not working correctly

Post by lervy »

Thanks everyone for looking at this problem for me, I just tried the same code out on my friends hosting and it worked with no problems, only difference is that his hosting is using PHP 5 where mine is using PHP 4....
Post Reply