Jump to content


Photo

$_REQUEST Problem


  • Please log in to reply
11 replies to this topic

#1 fixxxer

fixxxer
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 24 May 2006 - 12:41 PM

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.

#2 eves

eves
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 24 May 2006 - 01:10 PM

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]

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 May 2006 - 01:14 PM

No the $_REQUEST variable only stores $_GET, $_POST and $_COOKIE data. Sessions are stored in their own seperate variable $_SESSION

#4 fixxxer

fixxxer
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 24 May 2006 - 01:26 PM

[!--quoteo(post=376650:date=May 24 2006, 08:10 AM:name=eves)--][div class=\'quotetop\']QUOTE(eves @ May 24 2006, 08:10 AM) View Post[/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) View Post[/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

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 May 2006 - 01:30 PM

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.

#6 fixxxer

fixxxer
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 24 May 2006 - 02:48 PM

[!--quoteo(post=376659:date=May 24 2006, 08:30 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ May 24 2006, 08:30 AM) View Post[/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

#7 eves

eves
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 24 May 2006 - 03:06 PM

[!--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:
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'];
}


#8 fixxxer

fixxxer
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 24 May 2006 - 04:43 PM

[!--quoteo(post=376695:date=May 24 2006, 10:06 AM:name=eves)--][div class=\'quotetop\']QUOTE(eves @ May 24 2006, 10:06 AM) View Post[/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:
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'];
}
[/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?

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 May 2006 - 05:17 PM

You'll want to do this instead:
//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");
}


#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 05:57 PM

isn't that code a bit redundant, wildteen?
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");
}
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.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 fixxxer

fixxxer
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 24 May 2006 - 06:08 PM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
isn't that code a bit redundant, wildteen?
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");
}
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



#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 06:25 PM

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\" /]

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");
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users