Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use store and use session variables across pages?

Tags:

When one page is accessed, I would like to start a session and store a session variable:

<?php   session_start();    $_SESSION['myvar']='myvalue'; ?> 

Then from another page, I would like to check if that session variable has been stored:

<?php     session_start();     echo("1");     if(isset($_SESSION['myvar']))     {         echo("2");        if($_SESSION['myvar'] == 'myvalue')        {            echo("3");            exit;        }     }     ?> 

This code does not work for me.

like image 975
ab11 Avatar asked Mar 30 '11 16:03

ab11


People also ask

How do I use a session on a different page?

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. Show activity on this post. Check that the session cookie is being sent to the browser on the first hit and getting returned by the browser in subsequent requests.

How do you store values in session variables?

Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

How can we use session variable in another page in PHP?

Use the session_start Function This is the method that you'll see most often, where a session is started by the session_start function. The important thing is that the session_start function must be called at the beginning of the script, before any output is sent to the browser.

How do you use session variables?

Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application.


2 Answers

Sessions Step By Step

  1. Defining session before everything, No output should be before that, NO OUTPUT

    <?php session_start(); ?> 
  2. Set your session inside a page and then you have access in that page. For example this is page 1.php

    <?php    //This is page 1 and then we will use session that defined from this page:     session_start();     $_SESSION['email']='[email protected]'; ?> 
  3. Using and Getting session in 2.php

     <?php  //In this page I am going to use session:    session_start();   if($_SESSION['email']){   echo 'Your Email Is Here!  :) ';   }  ?> 

NOTE: Comments don't have output.

like image 99
Mohammad Kermani Avatar answered Mar 06 '23 08:03

Mohammad Kermani


All you want to do is write --- session_start(); ----- on both pages..

<!-- first page --> <?php   session_start();    $_SESSION['myvar'] = 'hello'; ?>  <!-- second page --> <?php     session_start();     echo $_SESSION['myvar']; // it will print hello   ?> 
like image 33
gaurav Avatar answered Mar 06 '23 06:03

gaurav