SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing items in a session

    How would I store this in a session:

    <?php
    if(isset($_GET['gassearch'])){
    echo $_GET['gassearch'];}
    else{ echo $_GET['gassearch'] = 'not ordered';}
    ?>

    I need to access this info on another page if it's possible.

    Many thanks

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just add to $_SESSION['gassearch']

    if(isset($_GET['gassearch'])){
    $_SESSION['gassearch'] = $_GET['gassearch'];

    }

    then during user's session you can use $_SESSION['gassearch'] on any page
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to extend, add

    PHP Code:
    session_start();
    /* this needs to be at the very top of every page you 
    want to access $_SESSION data */

    if(isset($_GET['gassearch'])) {

         
    $_SESSION['gassearch'] = $_GET['gassearch'];

    }

    echo 
    $_SESSION['gassearch']; 

  4. #4
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for your replies.

    I'm trying to get the info out to send in an email but I'm struggling a bit:
    PHP Code:
    // multiple recipients
    $to  'example@hotmail.com' ', '// 
    $to .= 'example@hotmail.com';

    // subject
    $subject 'Ordered Items';

    // message
    $message '
    <html>
    <head>
    </head>
    <body>
    if(isset($_SESSION['
    gassearch'])){   
    echo $_SESSION['
    gassearch'] = $_SESSION['gassearch'];}
    else{ echo $_SESSION['
    gassearch'] = 'not ordered';}

    </body>
    </html>
    '
    ;

    // Mail it
    mail($to$subject$message$headers); 
    This is throwing up an error: Parse error: syntax error, unexpected T_STRING

  5. #5
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no wonder
    PHP Code:
    // message

    $message '<html>
    <head>
    </head>

    <body>'
    // come out of the variable here to do if statement

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

        
    /* i guess you want the variable in the mail: */
        
    $message .='<p>'$_SESSION['gassearch'].'</p>';

    } else { 

        
    $message .='<p>Not Ordered</p>';

    }



    $message .='</body></html>'
    and i think you are a little confused.

    you assign a session in the same way you assign a variable. You also access it the same (essentially).

    PHP Code:
    /* your code */
    echo $_SESSION['gassearch'] = $_SESSION['gassearch'];}
    # this is wrong, it should be:

    $_SESSION['my_session'] = 'some data'// in this example you want to use $_GET['gassearch'];
     
    echo $_SESSION['my_session'];
    // echos some data

    // and don't forget you *must* have session_start(); 
    // at the very top of the page or it won't work. 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •