6

Ok I set up a session... but now how do I make it work on my other pages?

I tried doing

@session_start();

if(isset($_SESSION['$userName'])) {

 echo "Your session is running " . $_SESSION['$userName'];

}
5
  • sorry for late responses the power went out in the area lol ^.^
    – MrEnder
    Commented Mar 30, 2010 at 3:07
  • 6
    Just to clarify - shouldn't "$_SESSION['$userName']" be $_SESSION['userName'] (without the dollar sign) ?
    – bisko
    Commented Mar 30, 2010 at 9:26
  • 3
    Why are you using @? Is session_start spitting out some errors or warnings?
    – Chris
    Commented Mar 30, 2010 at 9:59
  • Can you edit the question to add the code where you set the session Commented Mar 30, 2010 at 11:41
  • Note: it's not work with some php frameworks like Yii.. you have to use the offical session function for each framework
    – Abudayah
    Commented Feb 17, 2014 at 13:12

8 Answers 8

12

If your PHP setup is clear (session writing ok) and cookie normally sent to browser (and preserved), you should be able to do something like this

On first page :

session_start();
$_SESSION['userName'] = 'Root';

On a second page :

session_start();
if(isset($_SESSION['userName'])) {
  echo "Your session is running " . $_SESSION['userName'];
}

Be careful session_start() must be called before any output is sent, so if you had to use the @ for session_start it can hide warnings.

As these are warnings, if given example doesn't work try to add this before calling session_start :

error_reporting(E_ALL);
1
  • 1
    He is right: your session_start command must be at the very beginning of the Web page, before even the <html> tag, otherwise it will create a new session every time!
    – yannis
    Commented Jan 18, 2022 at 18:47
1

On the first page (test1.php),

<?php
session_start();

$_SESSION['userName'] = 'This is Ravi';

?>

On the second page (test2.php),

<?php
session_start();
if(isset($_SESSION['userName'])) {
echo "Your session is running " . $_SESSION['userName'];
}

?>
0

Make sure session_start() is at the top of every page you wish to use sessions on. Then you can refer to session variables just like in your example.

1
  • thats what ive done =[ its not working =[ must be a problem with my login
    – MrEnder
    Commented Mar 30, 2010 at 3:06
0

Check that the session cookie is being sent to the browser on the first hit and getting returned by the browser in subsequent requests. There are lots of reasons why this may be failing - typically PHP has flushed the headers before the call to session_start() (which may be due to UTF-8 BOM chars or just messy programming).

Make sure you've got error reporting enabled.

C.

0

Make sure both pages are on the same domain. Even www.site.com is different than site.com

Using echo session_id(); also helps identifying your session_id on each page, make sure there are the same

0

Be aware of case sensitivity in the $_session name variables.

Therefore $_SESSION['userName'] different with $_SESSION['username'].

1
  • 2
    This does not answer the question and as such should be a comment at most.
    – EWit
    Commented Aug 23, 2014 at 10:23
0
  1. Create a login page, the user must not login without correct id and passowrd.
  2. After logging in the user comes to the home, here user can logout and goes back to the login page

NOTE: User must not access home page without going through login page.

0

first page (hello1.php) - Storing Session

$userName = "Nick";
session_start();
$_SESSION['username'] = $userName;

second page (hello2.php) - Output Session

session_start();
$userName = $_SESSION['username'];
echo "$userName";

Output: Nick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.