Page 1 of 1

session not working

Posted: Sat Dec 05, 2009 11:38 am
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
?>

Re: session not working

Posted: Sat Dec 05, 2009 12:03 pm
by askbapi
can u give more info about ur process and output.

Re: session not working

Posted: Sat Dec 05, 2009 12:07 pm
by mianmajidali
when data_final.php file is called
it will show
"Sorry: You are not logged in"
and also not show the value of session id by echo

Re: session not working

Posted: Sat Dec 05, 2009 12:31 pm
by askbapi
mianmajidali wrote: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
?>

Take a look :
--------------------------------
$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/");
}

Do this :
--------------------------------------------------------
1> Put each "Session created"; just above $_SESSION['s_id']="1122";
2> put // header("Location: http://www.os-co.biz/data_final.php/");

if u don't see the echoed message, there is a problem in "mysql_num_rows($chk)", query give 0 row. U need check the query string.

Re: session not working

Posted: Sat Dec 05, 2009 12:46 pm
by mianmajidali
Listen plz
i have checked, on check_login.php file, $_SESSION gets value, but it does not transeferd to the $_SESSION used in data_final.php file.
i have confirmed it by using echo with successful login and wrong login
by this code
-----------------------------------------------
$chk = mysql_query("select * from members where name='".$_POST['name']."' and pass='".$_POST['pass']."'");

if(mysql_num_rows($chk)>0)
{

session_register('s_id');
$_SESSION['s_id']="1122";
//header("Location: http://www.os-co.biz/data_final.php/");
echo $_SESSION['s_id'];
}



else
{
$_SESSION['s_id']=session_regenerate_id();
echo $_SESSION['s_id'];
session_destroy();
echo "</br>";
echo $_SESSION['s_id'];
echo "</br>";
echo "Wrong Username or Password:";
}

Re: session not working

Posted: Sun Dec 06, 2009 8:44 am
by askbapi
Do this test to check :

Create two files with following

index.php
--------------------------------
<?php
session_start();

$_SESSION['s_id']="1122";
header("Location: http://localhost/data_final.php/");

?>

data_final.php
------------------------------
<?php
session_start();

echo $_SESSION['s_id'];

?>

if u see the output "1122", than every thing is Ok else check ur server setting.

by the way what serve r u using?