$_SESSION[] not getting value

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
mianmajidali
Forum Commoner
Posts: 30
Joined: Tue Dec 01, 2009 8:05 pm

$_SESSION[] not getting value

Post by mianmajidali »

hi to all
i m using session in login and want to display database only members, i m using session id for this, but my database page cant get the session value.
i m giving code behind, please check and help
-------------------------------------------------------------------
this is check_login.php file
---------------------------------------------------
<?php
session_start();
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("oscobizz_os", $con);
$chk = mysql_query("select * from members where name='".$_POST['name']."' and pass='".$_POST['pass']."'");
if(mysql_num_rows($chk)>0)
{
$_SESSION['s_id']="1122";
header("Location: http://www.os-co.biz/data_final.php/");
}
else
echo "Wrong Username or Password:";
?>
----------------------------------------------------------------
this is database file named as data_final.php
-----------------------------------------------------------------
<?php
session_start();
echo $_SESSION['s_id'];
if($_SESSION['s_id']!=1122)
echo "Sorry: You are not logged in";
else
{
$con = mysql_connect("localhost","username","password");
if (!$con)
die('Could not connect: ' . mysql_error());
else
{
mysql_select_db("oscobizz_os", $con);
$result = mysql_query("SELECT * FROM query");
while($row = mysql_fetch_array($result))
{
echo "<html>";
echo "<body>";
echo "<table>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['tel'] . "</td>";
echo "<td>" . $row['fax'] . "</td>";
echo "<td>" . $row['mob'] . "</td>";
echo "<td>" . $row['box'] . "</td>";
echo "<td>" . $row['query']."</td>";
echo "</tr>";
echo "</table>";
echo "</body>";
echo "</html>";
}//end of while
} //end of inner else
}//end of main else
?>
------------------------------------------------------------
when i provide the correct login info, and header calls the data_final.php file, it does not show the result, and display the msg "Sorry: You are not logged in" and also does not show the $_SESSION value by echo.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SESSION[] not getting value

Post by AbraCadaver »

Try:

Code: Select all

header("Location: http://www.os-co.biz/data_final.php/");
exit;
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: $_SESSION[] not getting value

Post by daedalus__ »

dont use header to redirect.

use meta tags
Katsu Webs
Forum Newbie
Posts: 5
Joined: Fri Dec 04, 2009 2:20 pm

Re: $_SESSION[] not getting value

Post by Katsu Webs »

I dont know how helpful this is, but this is a snippet from one of my projects;


This code is from the login script, it takes $username2 from $_POST. Its been cleaned and varified before being check in the database and put in the session.

Code: Select all

$finddata = "SELECT * FROM users WHERE username='$username2' AND password='$password2'";
    $sendquery = MYSQLI_QUERY($cxn,$finddata) or die ('Database Unavailable');
    if(MYSQLI_NUM_ROWS($sendquery) > 0)
        {
        $_SESSION['user'] = $username2;
        echo "<html><head><meta http-equiv='REFRESH' content='0;url=http://localhost/Members.php'></head><body></body></html>";
        }
        else
            {
            echo "<html><head><meta http-equiv='REFRESH' content='0;url=http://localhost/loginfail.php'></head><body></body></html>";
            exit();
            }
Note the meta tags to redirect, which stops you from getting a parse error due to using header() after outputing values. Also note I start the session as soon as they go to the login page, before any other code,but only set the session to $_SESSION['user'] if username and password match their database entries.

This is the session authorisation code from the members page it redirects to.

Code: Select all

session_start();    
    if(!isset($_SESSION['user']))
        {
        echo"<html><head><meta http-equiv='REFRESH' content='0;url=http://localhost/loginfail2.php'></head><body></body></html>";
        }
I check the named session is set, this goes above all the html in the page. I had this same issue, the above code now works.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SESSION[] not getting value

Post by AbraCadaver »

daedalus__ wrote:dont use header to redirect.

use meta tags
Not if you want predictable results in all browsers. Also, use of meta refresh is discouraged by the W3C.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply