fixxxer Posted May 24, 2006 Share Posted May 24, 2006 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 wellHas 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. Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/ Share on other sites More sharing options...
eves Posted May 24, 2006 Share Posted May 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38548 Share on other sites More sharing options...
wildteen88 Posted May 24, 2006 Share Posted May 24, 2006 No the $_REQUEST variable only stores $_GET, $_POST and $_COOKIE data. Sessions are stored in their own seperate variable $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38549 Share on other sites More sharing options...
fixxxer Posted May 24, 2006 Author Share Posted May 24, 2006 [!--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.gin my script i set $_SESSION['game_id'] = 4 //or whatever game isthen 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 Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38553 Share on other sites More sharing options...
wildteen88 Posted May 24, 2006 Share Posted May 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38557 Share on other sites More sharing options...
fixxxer Posted May 24, 2006 Author Share Posted May 24, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38585 Share on other sites More sharing options...
eves Posted May 24, 2006 Share Posted May 24, 2006 [!--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 $_COOKIEthis 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 Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38593 Share on other sites More sharing options...
fixxxer Posted May 24, 2006 Author Share Posted May 24, 2006 [!--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 $_COOKIEthis 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? Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38629 Share on other sites More sharing options...
wildteen88 Posted May 24, 2006 Share Posted May 24, 2006 You'll want to do this instead:[code]//check whether game id is set in the session/post variables firstif(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] Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38641 Share on other sites More sharing options...
.josh Posted May 24, 2006 Share Posted May 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38657 Share on other sites More sharing options...
fixxxer Posted May 24, 2006 Author Share Posted May 24, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38664 Share on other sites More sharing options...
.josh Posted May 24, 2006 Share Posted May 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/10342-_request-problem/#findComment-38666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.