...

View Full Version : Session is not working.........why?



mds8000
08-02-2007, 09:28 PM
hi friends,
i wrote a simple login script. i created 3 pages login.php,loginck.php and welcome.php.........first the user enters into login.php. he should press the submit button when he fills out the username and password fields........after that, the page redirects to loginck.php........validations are done here.........i received the field values by $_POST['xxxx'];........then i have stored that value in a session(in loginck.php).........on successful login,page redirects to welcome.php.......in this page i want to display the user name.......i tried echo $_session['xxxx'];.....but it is not working.........why.....pls help me ........its urgent.......
coding for welcome.php___________
<?php
@session_start();

?>


<html>

<head>
<title>WELCOME</title>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

welcome<?php

echo $_session['xxxx'];

?>

</body>

</html>
pls help me........but i created 2 pages which a string is stored in session at first page then it s displayed in the second page........it s working........but.......wat happen to the above program?

give me one sample script which gets a value from a text field in first page and then to display that value in the third page through second page(not directly from first).....

_Aerospace_Eng_
08-02-2007, 10:30 PM
Post the code for loginchk.php and login.php

Also learn to start typing proper sentences. This ... stuff makes it hard to understand you.

mds8000
08-03-2007, 03:21 PM
sorry for my awkward sentences. yesterday i was very tensed due to this session issue. that's the reason which made me crazy to find out the solution.
again sorry for that. go through the codes.........
login.php

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Login</title>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<form action='loginck.php' method=post name="form2">
<table border='0' cellspacing='0' cellpadding='0' align=center>
<tr id='cat'>
<tr> <td ><font face='verdana, arial, helvetica' size='2' align='center'> &nbsp;Login ID &nbsp; &nbsp;
</font></td> <td align='center'><font face='verdana, arial, helvetica' size='2' >
<input type ='text' class='bginput' name='userid' ></font></td></tr>

<tr> <td ><font face='verdana, arial, helvetica' size='2' align='center'> &nbsp;Password
</font></td> <td align='center'><font face='verdana, arial, helvetica' size='2' >
<input type ='text' class='bginput' name='password' ></font></td></tr>

<tr> <td colspan='2' align='center'><font face='verdana, arial, helvetica' size='2' align='center'>
<input type='submit' value='Submit'> <input type='reset' value='Reset'>
</font></td> </tr>


<tr> <td ><font face='verdana, arial, helvetica' size='2' align='center'> &nbsp;<a href='signup.php'>New Member Sign UP</a></font></td> <td bgcolor='#ffffff' align='center'><font face='verdana, arial, helvetica' size='2' >
Forgot Password ?</font></td></tr>

<tr> <td colspan='2' align='center'><font face='verdana, arial, helvetica' size='2' align='center'>
&nbsp;</font></td> </tr>


</table></center></form>

</body>

</html>


loginck.php
<?php
@session_start();

$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='sa';
// name of database
$dbname='signup';

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $con;
$con=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$con){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$con) or die ("could not open db".mysql_error());
}

?>

<html>

<head>
<title>Login</title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?php
$userid=mysql_real_escape_string($userid);
$password=mysql_real_escape_string($password);

if($rec=mysql_fetch_array(mysql_query("SELECT * FROM login WHERE userid='$userid' AND password =

'$password'"))){
if(($rec['userid']==$userid)&&($rec['password']==$password))
{
$_session['id']=session_id();
$_session['userid']=$_POST['userid'];
echo $_session['userid'];


print "<script>";
print " self.location='welcome.php';"; // auto-redirect
print "</script>";

}
}
else {

echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Userid

and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";

}
?>
<p class=data> <center>Successfully,Logged in<br><br><a href=welcome.php>Click here if your browser is

not redirecting automatically or you don't want to wait.</a><br></center>
</body>

</html>

welcome.php
<?php
@session_start();
if(isset($_SESSION['userid']))
{
echo "session is open. Welcome";
echo $_session['userid'];
}
else
{
echo "session is not open";
}



?>


---------------------------------------

CFMaBiSmAd
08-03-2007, 04:53 PM
Variable names are case sensitive. You have an inconsistent mix of $_SESSION['userid'] and $_session['userid']. Only $_SESSION[...] refers to an actual session variable.

Don't suppress error reporting using an @, especially on session_start(). If it is failing due to an error, it won't work and you will never know the reason why.

mds8000
08-03-2007, 06:12 PM
it is showing two warning's on two pages (loginck.php , welcome.php), when i removed "@" before session_start() (also removed "session" with "SESSION")

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\loginck.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\loginck.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\loginck.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\loginck.php on line 2


i'm using latest version of APACHE.

_Aerospace_Eng_
08-03-2007, 06:15 PM
Make sure there is no whitespace above the <?php part in loginchk and welcome.php.

mds8000
08-03-2007, 06:23 PM
there is no whitespace above <?php part...........

CFMaBiSmAd
08-03-2007, 06:35 PM
It is likely that the files are being saved in a Unicode/UTF-8 format, in which case some non-printing/binary characters are being output prior to the <?php. Check your editor "save as" file type setting for those files and make sure it is ANSI/ASCII.

mds8000
08-03-2007, 06:47 PM
thank you soooooooooo much CFMaBiSmAd.now it is perfectly working.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum