Maurice Greenland Maurice Greenland - 1 year ago 79
PHP Question

PHP Session SID URL - url does not have SID

I'm trying to learn how to use Sessions to maintain state between pages.

I think my problem is that I dont know how to escape properly. When I load the page im not getting the SID at the end of the url, instead I'm getting this:<?php echo SID;?>

I have session_start() at the top of every page. And the session initialy works as I see the login name appear on the page and the Register and Login links, but then when I click to another page the session is not maintained.

The main site nav is dynamically createdusing an array($nav) and foreach loop. Here too I'm not sure how to echo the url and php SID constant. I don't think my syntax is correct.

echo "<nav id='statusBar'>";

echo "<ul>";

if(isset($_SESSION['userIn']) && $_SESSION['userIn'] != ''){

echo "<li id='userLo'>Log Out</li>";
echo "<li id='userLi'>User Logged In: " . $_SESSION['userIn'] . "</li>";

} else{

echo '<li><a href="registration.php?<?php echo SID;?>">Register</a></li>';

echo '<li><a href="login.php?<?php echo SID;?>">Login</a></li>';


echo "</ul>";

echo "</nav>";

echo "<nav id='nav'> ";

$nav = array();

$nav['index.php?<?php echo SID;?>'] = 'Home';
$nav['public1.php?<?php echo SID;?>'] = 'Public 1';
$nav['public2.php?<?php echo SID;?>'] = 'Public 2';
$nav['members.php?<?php echo SID;?>'] = 'Members';

echo '<ul>';

foreach ($nav as $key => $navValues) {
echo "<li>";
echo "<a href='$key'>$navValues</a>";
echo "</li>";

echo '</ul>';

echo "</nav>";

Answer Source

Below everything within echo '..'; is interpreted as html.

echo '<li><a href="login.php?<?php echo SID;?>">Login</a></li>';

If you want to use variables inside the string you have to concatenate them like so:

echo '<a href="login.php?' . session_name() . ' ='  . session_id() . ' ">Login</a>' ;

For concatenating php code take a look at: and more about sessions:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download