Page 1 of 1

session variable

Posted: Mon Aug 27, 2007 10:18 am
by danarashad
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


how do you maintain session variables.  i set the variable in a login page, then when i select a link away from the page, my session variable dies.  
here's some code.
variable gets set here
this is my login check page

Code: Select all

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
$_SESSION['views']="$myusername";
$_SESSION['id'] = "$id";

while ($row = mysql_fetch_assoc($result)) {
	$_SESSION['id']="$row[id]";
	echo $_SESSION['id'];
}

gets redirected to home page 
<? session_start();
if(isset($_SESSION['views']))
{}
else{
header("location:login.php");
}
?>
<?php include ("header.php"); ?>
<?php
 //retrieve session data
 echo $_SESSION['views']; // echo's the session which is a username
?><br>
i also include a menu page. which has 3 different links. when i select the link, the session variable no longer displays. help please? i dont know how to keep the session across different pages.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Aug 27, 2007 10:59 am
by Zoxive
Is session_start() on top of every page?

start session

Posted: Mon Aug 27, 2007 11:09 am
by danarashad
no, i take it has to be at the top.

Posted: Mon Aug 27, 2007 11:12 am
by Zoxive
Yes, before anything is output to the browser.

thanks

Posted: Mon Aug 27, 2007 1:32 pm
by danarashad
thank you for your help, it works, would it be better. i put that start section into an include file, and include it with all pages?
thanks again man, i am a Coldfusion program, and i am trying to learn PHP. thanks man.

Posted: Mon Aug 27, 2007 3:09 pm
by Kieran Huggins
there's also a php.ini directive called "session_autostart", but it has some caveats.

You could also use "auto_prepend_file" with a session_start() hidden away in there