...

View Full Version : Using session data



Stevesparty
09-01-2007, 05:53 AM
I'm am trying to use sessions to send user data between web pages on my site.
The page starts by receiving form information from another page:

$name = $_POST['userid'];
It then uses that data to make the session variable "$session['name']":


session_start() ;
$session['id']=session_id();
$session['name']= $name;

The problem here is that I want to be able to use the variable: "$session['name']" on another page but don't really know how. Any help would be appreciated. Thanks!

Iszak
09-01-2007, 06:25 AM
Well firstly you could use a predefined variable to store it like $_SESSION... so like


session_start();
$_SESSION['id'] = session_id();
$_SESSION['name'] = $name;


and if I remember as long as session_start is called every time you need the data it'll be their unless they clear it. So you'll probably want a check to make sure they haven't cleared it somehow.. anyhow good luke.

Stevesparty
09-01-2007, 06:36 AM
This is my page which tries to draw the session data:

<?php
session_start();
$session['id']=session_id();
$name = $session['name'];

echo $name;
?>

You might be right about it clearing the data but I can't see where it would be doing so. Here is a bit more of the code from the session start page:


if ($psswrd == $password)
{session_start() ;
$session['id']=session_id();
$session['name']= $name;
echo "<p class=data> <center>Successfully,Logged in<br>
<br><a href='logout.php'> Log OUT </a><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>";
}
else { session_unset();
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>";}

}

PappaJohn
09-01-2007, 08:40 AM
$_SESSION['id'] = session_id();

$session['id']=session_id();

These are not the same things.

Stevesparty
09-03-2007, 12:40 AM
This still isn't working, I believe that this is because the sessions are not setting correctly. I found this session test page from a tutorial website:


<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>

When I refresh the page it should increase the page view, this, however, doesn't happen. I think what is happening is that the page is thinking that the session is not set and therefore moving on to the "else" clause down below. If anyone knows anything that could help me then please say so as I am totally lost at this point.
Thanks again.

JordanW
09-03-2007, 01:23 AM
It could simply be that your host has disabled Sessions. To check this you need to view your PHP.INI file by creating a PHP file with the code:



<?PHP
echo phpinfo()
?>


Then open the page in your browser and scroll down to Session the first field "Session Support" should hopefully say enabled... otherwise it is disabled, and you won't be able to use them.


~Jordan

Nightfire
09-03-2007, 01:44 AM
Just a quick tip, no need to use echo for phpinfo, as the function already echo's stuff out. Just



<?php
phpinfo();
?>

is enough :)

Stevesparty
09-03-2007, 02:27 AM
Session support is enabled so that doesn't seem to be the problem. At one point I was able to get a session test to work however I was not able to repeat this test. The problem could lie somewhere in my code but after checking and rechecking I don't see a problem.
Here is the page which starts the session:

<html>
<head>
<?php


$name = $_POST['userid'];
$password = $_POST['password'];

$flag="OK";
$msg="";

if(strlen($name) < 5){
$msg=$msg."( Please enter user id more than 5 character length )<BR>";
$flag="NOTOK";
}

if(strlen($password) < 5 ){
$msg=$msg."( Please enter password of more than 5 character length )<BR>";
$flag="NOTOK";
}

if(ereg('[^A-Za-z]', $name)){
$msg=$msg."( Please use only alphabets a to z as userid )<BR>";
$flag="NOTOK";
}

if($flag <>"OK"){
echo "<center>$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'></center>";
}else{

mysql_connect("XXXXX", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("stats") or die(mysql_error());

$result = mysql_query("SELECT * FROM $name") or die(mysql_error());

$row = mysql_fetch_array($result);

$psswrd = $row['password'];

if ($psswrd == $password)
{session_start() ;
$_SESSION['name'] = $name;
echo "<p class=data> <center>Successfully,Logged in<br>
<br><a href='logout.php'> Log OUT </a><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>";
print "<script>";
print " self.location='welcome.php';";
print "</script>";
}
else { session_unset();
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>";}


}
?>
</head>
<body>
</body>
</html>


And here is the page which is supposed to display the session variable:


<?php
session_start();


echo "hello";
echo $_SESSION['name'];


?>

Once again any help would be appreciated.

Nightfire
09-03-2007, 09:00 AM
session_start(); is meant to be before any output to the browser. You have it near the end of the page



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum