session variable

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
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

session variable

Post 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]
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Is session_start() on top of every page?
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

start session

Post by danarashad »

no, i take it has to be at the top.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Yes, before anything is output to the browser.
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

thanks

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
Post Reply