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

$_SESSION["username"]=$username;

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.

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

page1.php

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

page2.php

<?php
session_start();
var_dump($_SESSION["username"]);
//phpinfo();
?>

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"].

>

Member Avatar for diafol

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.

$_SESSION['username"]

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

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.
$uname=$_POST['username'];
$_SESSION["username"]=$uname;
it will store result for this page but won't be available for other pages.

more robust method is
$_SESSION["username"]=$_POST['username'];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.