1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Using $_SESSION variables

Discussion in 'Web Design & Development' started by JiminSA, Dec 21, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    PHP:
    session_start();

    if (isset(
    $_REQUEST['id'])) 
    {
        
    $id $_REQUEST['id'];
    }
    else
    {
        
    $id "";
    }

    $_SESSION['ad_id'] = $id;
    echo 
    "AD_ID " $_SESSION['ad_id'];
    This code produces the following
    i.e. the variable $id is being treated as a literal:confused:
    Obviously I am not treating session variables correctly, but cannot for the life of me see how:(
     
  2. colinsp

    colinsp

    Joined:
    Sep 5, 2007
    Messages:
    2,302
    First Name:
    Colin
    Jim

    Why are you using $_REQUEST it is often easier to use $_POST or $_GET?
     
  3. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    I read in a tutorial somewhere that it was preferable to use $_REQUEST as it handles both url and form data retrieval
    and other pros if you Google $_REQUEST vs $_GET $_POST
     
  4. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    696
    Using $_REQUEST can be a security risk if you use it blindly... for example form data that you assume is coming from $_POST (or $_COOKIE, for that matter) but instead is sent with malicious intent by $_GET could get you some unexpected results (think SQL injection). If you are doing anything that will ultimately interact with a database on the back end, then it's better to assign based on *exactly* what you are expecting. Otherwise, $_REQUEST can be a great way of being generic or even testing code.

    To the original question... are you sure that somehow $_REQUEST is not set to the literal string "$id"? I would echo out the individual components and see what you get:

    Code:
    echo "GET = ".$_GET."<br>";
    echo "POST = ".$_POST."<br>";
    echo "COOKIE = ".$_COOKIE;
    using var_dump would probably be more useful than echo...
     
  5. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    Thanks for the heads-up on $_REQUEST :) and yes 'twas my bad - putting ?id='$id' (note single quotes) in a URL link:mad: and thank you also Colin - Season's Greetings to all(y)
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1081824

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice