Solved: Using $_SESSION variables

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

JiminSA

Jim
Thread Starter
Joined
Dec 15, 2011
Messages
3,407
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
AD_ID $id
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:(
 

colinsp

Colin
Joined
Sep 5, 2007
Messages
2,356
Jim

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

JiminSA

Jim
Thread Starter
Joined
Dec 15, 2011
Messages
3,407
I read in a tutorial somewhere that it was preferable to use $_REQUEST as it handles both url and form data retrieval
$_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self referential forms for validations.
and other pros if you Google $_REQUEST vs $_GET $_POST
 
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...
 

JiminSA

Jim
Thread Starter
Joined
Dec 15, 2011
Messages
3,407
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)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top