i am ensuring on both pages i have first of all called session_start();
on one page i declared


the link of another page is on this page.
and another page i tried to print it by

echo $_SESSION["username"];

but it showed nothing.
i am using php 5.4 with apache and mysql.
lookin for your reply. thanks in advance.

Edited by diafol: applied code tags

5 Years
Discussion Span
Last Post by nchy13

Just to confirm it should be like this: ($username maybe not set?)


$_SESSION["username"] = 'Testing';
echo '$_SESSION["username"] set to Testing'."<br/>\r\n";
echo "<a href='page2.php'>page 2</a>";



If it's not working uncomment the phpinfo(); that will tell you a list of the php settings and see what it has set for sessions


i have one small doubt that when to use 'single inverted comma' and 'double inverted comma' with echo. i always see them being used randomly and havn't got the idea of it's usage.

on first page $_session["username"] was set successfully.
i checked it with the help of var_dump();.
but on linked page it was not retaining it's value.
i need to set variable to $_session["username"].



Single quotes and double quotes are often interchangeable in PHP. However, ypu must use them in pairs - don't start with one and finish with another, e.g.


That would be wrong. You have to be careful when echoing/printing - they can behave differently in this case.

Edited by diafol


thanx for your reply. have a look at the second part of my previous post.
can you suggest any reason for the non accessibility of $_SESSION["username"] variable.


finally i got it after wasting around 4 hours for this problem. now i can continue with my project without stoppage
i still have one doubt what if i didn't have $_POST and wanted to assign a variable to $_SESSION.why it won't work.
it is wrong habit to do like this as value of $_SESSION won't be available across pages.
it will store result for this page but won't be available for other pages.

more robust method is

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.