Jump to content

Archived

This topic is now archived and is closed to further replies.

fixxxer

$_REQUEST Problem

Recommended Posts

Im new to php and have been been building a mock CMS to try out what ive learnt. In the book i was learning from it said that using $_REQUEST i could get SESSION, GET and POST variables using $_REQUEST, but when i use the $_REQUEST['variable'] the variable is never there while if i use $_SESSION['variable'] the variable is fine. in this case the variable was a session but i also wanted to be able to set from POST as well
Has the use of $_REQUEST changed since the book im working from was published or am i doing something wrong, ive looked on the net but cant seem to find anything.

If anyone can help. Thanks.

Share this post


Link to post
Share on other sites
check the manual:
[a href=\"http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server\" target=\"_blank\"]http://www.php.net/manual/en/reserved.vari...ariables.server[/a]

Share this post


Link to post
Share on other sites
No the $_REQUEST variable only stores $_GET, $_POST and $_COOKIE data. Sessions are stored in their own seperate variable $_SESSION

Share this post


Link to post
Share on other sites
[!--quoteo(post=376650:date=May 24 2006, 08:10 AM:name=eves)--][div class=\'quotetop\']QUOTE(eves @ May 24 2006, 08:10 AM) [snapback]376650[/snapback][/div][div class=\'quotemain\'][!--quotec--]
check the manual:
[a href=\"http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server\" target=\"_blank\"]http://www.php.net/manual/en/reserved.vari...ariables.server[/a]
[/quote]

thanks, but i had read it before and to be honest wasn't quite sure on what it meant. According to that it means that using $_SESSON['variable'] or $_REQUEST['variable'] should have the same affect is that correct?

e.g

in my script i set $_SESSION['game_id'] = 4 //or whatever game is

then when i come to retrieve that on my next page if my select it using $_SESSION['game_id'] its fine but if i select it using $_REQUEST['game_id'] the number isn't there. Can anybody explain why this occurs. could it have anything to do with my php server settings? Again any help would be appreciated, if it does make a difference im using XAMPP.

its just annoying cos i wanted the game id to be mainly set by a session vaiable but i also wanted to be able to set it using $_GET

thanks




[!--quoteo(post=376651:date=May 24 2006, 08:14 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ May 24 2006, 08:14 AM) [snapback]376651[/snapback][/div][div class=\'quotemain\'][!--quotec--]
No the $_REQUEST variable only stores $_GET, $_POST and $_COOKIE data. Sessions are stored in their own seperate variable $_SESSION
[/quote]

thanks thats what i needed. Also apologies to the person who wrote the earlier reply, you were correct and i misread it. thanks.


one further thing though according to the book ive been learning from it stated that SESSION variables were stored in $_REQUEST. is this something that has changed recently or is the book a pile of crap and only good for burning.

thanks everybody, youve been a great help, even if you've all proved im an idiot. lol

Share this post


Link to post
Share on other sites
Either you have misread the book, or the book as you said is a pile of crap! [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

I always use $_POST, $_GET and $_COOKIE rather than $_REQUEST.

Share this post


Link to post
Share on other sites
[!--quoteo(post=376659:date=May 24 2006, 08:30 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ May 24 2006, 08:30 AM) [snapback]376659[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Either you have misread the book, or the book as you said is a pile of crap! [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

I always use $_POST, $_GET and $_COOKIE rather than $_REQUEST.
[/quote]

thanks again.

this gives me a problem though, i have a script which selects the current game_id and then retrieves the complete row from the MySQL, in the script i have at the moment i wanted to be able to get the game_id from the session or from post. even though it was wrong. lol. in my head $_REQUEST allowed me to do this. is there any easy way i can still get the info from either $_POST and $_SESSION in one script.

Any help u can offer would be appreciated and thanks to everybody whose helped me so far

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
According to that it means that using $_SESSON['variable'] or $_REQUEST['variable'] should have the same affect
[/quote]

this is incorrect, the $_REQUEST only consist of the contents of $_GET, $_POST, and $_COOKIE
this will never be true: $_SESSON['variable'] == $_REQUEST['variable'] (unless of course, they change the implementation on PHP [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] )

for getting the value of the game id, test for the variable if it is set and has something on it, something like this:
[code]
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='')
{
    //just included the strip_tags() to clean posted entry
    //you might    want to add some more
    $game_id = strip_tags($_POST['game_id']);
}
else
{
    $game_id = $_SESSION['game_id'];
}
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=376695:date=May 24 2006, 10:06 AM:name=eves)--][div class=\'quotetop\']QUOTE(eves @ May 24 2006, 10:06 AM) [snapback]376695[/snapback][/div][div class=\'quotemain\'][!--quotec--]
this is incorrect, the $_REQUEST only consist of the contents of $_GET, $_POST, and $_COOKIE
this will never be true: $_SESSON['variable'] == $_REQUEST['variable'] (unless of course, they change the implementation on PHP [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] )

for getting the value of the game id, test for the variable if it is set and has something on it, something like this:
[code]
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='')
{
    //just included the strip_tags() to clean posted entry
    //you might    want to add some more
    $game_id = strip_tags($_POST['game_id']);
}
else
{
    $game_id = $_SESSION['game_id'];
}
[/code]
[/quote]
thanks, ive changed the script the only other problem that this script presents is that what happens if neither variable is set. for example someone ends up there by mistake POST and SESSiION variables aren't set. At the moment my script echos a no game selected.
Would i get round this by turning the else into a similar statement as the if

e.g.
else {
if ($_SERVER['REQUEST_METHOD']=='SESSION' &&isset($_SESSION['game_id]) && trim($_POST['game_id'])!='')
{
$game_id = $_SESSION['game_id'];
}
else
{
echo 'no game selected';
}


to me that addition would mean that if both SESSION and POST were not set a message telling the user no game was selected would be displayed?

Share this post


Link to post
Share on other sites
You'll want to do this instead:
[code]//check whether game id is set in the session/post variables first
if(isset($_SESSION['game_id']) || isset($_POST['game_id']))
{
    if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='')
    {
        //just included the strip_tags() to clean posted entry
        //you might    want to add some more
        $game_id = strip_tags($_POST['game_id']);
    }
    else
    {
        $game_id = $_SESSION['game_id'];
    }
}
else
{
    //kill the script no game is selected
    die("You have not selected a game! Please go back and select a game");
}[/code]

Share this post


Link to post
Share on other sites
isn't that code a bit redundant, wildteen?
[code]
if(isset($_POST['game_id'])) {
    if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='')
    {
        //just included the strip_tags() to clean posted entry
        //you might    want to add some more
        $game_id = strip_tags($_POST['game_id']);
    }
} elseif (isset($_SESSION['game_id']) {
        $game_id = $_SESSION['game_id'];
} else {
    //kill the script no game is selected
    die("You have not selected a game! Please go back and select a game");
}
[/code]
of course, you would want to switch the order of the conditions, depending on which you want it to check first. That is, if getting it from POST is more important to you than getting it from the session then that order is fine. But if you wish game_id to be set based on the session if it's there, and if not, then see if there is a post, then switch it.

Share this post


Link to post
Share on other sites
[!--quoteo(post=376760:date=May 24 2006, 12:57 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 24 2006, 12:57 PM) [snapback]376760[/snapback][/div][div class=\'quotemain\'][!--quotec--]
isn't that code a bit redundant, wildteen?
[code]
if(isset($_POST['game_id'])) {
    if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='')
    {
        //just included the strip_tags() to clean posted entry
        //you might    want to add some more
        $game_id = strip_tags($_POST['game_id']);
    }
} elseif (isset($_SESSION['game_id']) {
        $game_id = $_SESSION['game_id'];
} else {
    //kill the script no game is selected
    die("You have not selected a game! Please go back and select a game");
}
[/code]
of course, you would want to switch the order of the conditions, depending on which you want it to check first. That is, if getting it from POST is more important to you than getting it from the session then that order is fine. But if you wish game_id to be set based on the session if it's there, and if not, then see if there is a post, then switch it.
[/quote]

thanks guys. thats exactly what i was looking for, and even better i actually understand it. lol. looking over what youve done ive realised i can change a lot of the code on my site all thanks to the 'or' operater. all ive got to do now is find the damn key on my keyboard.

also thank god theres people like you lot out there to help newbies. ive no doubt ill be back with another question or you never know i might be able to answer somebody elses soon. lol

Share this post


Link to post
Share on other sites
the | is the symbol above your \ hold shift and press \

(assuming you have a 'standard' keyboard...)

actually, my code is a bit redundant too [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

[code]
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['game_id']) && trim($_POST['game_id'])!='') {
        //just included the strip_tags() to clean posted entry
        //you might    want to add some more
        $game_id = strip_tags($_POST['game_id']);
} elseif (isset($_SESSION['game_id']) {
        $game_id = $_SESSION['game_id'];
} else {
    //kill the script no game is selected
    die("You have not selected a game! Please go back and select a game");
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.