Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem reading PHP session variables in new page

Status
Not open for further replies.

benireland

Technical User
Joined
Apr 12, 2003
Messages
1
Location
GB
Ok I'm pretty much a novice to this! I installed the latest Apache, PHP and MYSQL to my PC running XP. I have been happily learning PHP/SQL from a fat book for several months and have learnt alot. Initial I didn't know how and then didn't get round to turning global_variables to 'on' in the c:\php\php.ini file. (To pass variable page-to-page I have been using: $_GET['var_name']).
Today I saved the file with this as 'on' but even afte a OS reset it hasn't made a difference that i've noticed w.r.t. collecting post/get form variables.

Now I am learning to use session variables. Which seem pretty straight forward but after I have set a login session variable (to allow personalised page content) my pages just won't detect the session variable even though I pulled the code out of a book.

I'd really appreciate it if someone could point out my error or at least a way I can narrow down the possible promlem. Here's the code;-

//////// extranct from login.php

{after I have valid dated the entered login details}...
if (session_is_registered("valid_user")){
echo &quot;you are now logged in as: $valid_user <br>&quot;;
echo &quot;<a href=\&quot;logout.php\&quot;> Log out </a> <br>&quot;;
}
<a href=&quot;cal_front.php&quot;> Members section </a>
... {At this stage the session variable is the correct value and this if proved in the echoed text. Then if we look at the file it links to;

//////// extracts from cal_front.php
<?
session_start();
if (session_is_registered(&quot;valid_user&quot;)){
echo &quot;A valid_user value is found in sess vars <br>&quot;;
}
else{
echo &quot;Warning: No valid_user in the sess vars <br> &quot;;
}

...{ but this time on a new page it doesn't detect this sess var.}

Can anyone help a desparate guy???? My wild guesses might be to do with the way I set (or rather did not set up) the server completely. Although just about everything else has worked.
Thanks very much
 
well, first of all in sessions you MUST start EVERY page you want to have session in with:

<?
session_start();
?>

without this it won't work at all.
next, i don't know abou that session_is_registered() function, i use:

if ($_SESSION[&quot;var_name&quot;] != &quot;&quot;) { u'r logged in } else {u'r not }

to assign values to each session var u use:

$_SESSION[&quot;varname&quot;] = &quot;bla bla bla&quot;;

remember, you MUST put session_start() before echoing anything/including/etc, the only things u can do before session_start are stuff like if clauses, db queries, things that don't influende your page output, at least at the moment you are doing them, maybe LATER they will, but we don't care about that.

so:

<?
session_start();

$_SESSION[&quot;user&quot;] = &quot;$nick&quot;;

if ($_SESSION[&quot;user&quot;] != &quot;&quot;) { echo &quot;welcome &quot;.$_SESSION[&quot;user&quot;].&quot; !&quot;; } else { echo &quot;please log in&quot;; }

i replyed based on &quot;...{ but this time on a new page it doesn't detect this sess var.}&quot;, if this wasn't what u asked, sorry, i missed u'r point :|
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top