phpSensei Posted August 27, 2007 Share Posted August 27, 2007 I have seen many of these in my address bar "http://www.website.com/index.php?1" How do coders retreive the "1", from the URL. i usually use the switch function. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 Usually it would be something like ?num=1 and they'd use $_GET['num']; I think to get the 1 in the situation you described you'd have to get the url using $_SERVER['REQUEST_URI'] and then a string manipulation function to get the values after the ? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 27, 2007 Share Posted August 27, 2007 I think you'd go for $_SERVER["QUERY_STRING"] - since this contains only the text after the ? It always seems like an ugly method to me though - as jesirose said, you're better off putting the variable in the URL and retrieving it from the GET array. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 You can get them by looping over the $_GET array... http://website.com/index.php?1 The above URL has a GET argument called 1 and its value is ''. Try this: <?php if($_GET) { foreach($_GET as $key => $value) { echo "Key: " . $key . "<br>"; echo "Value: " . $value . "<br><br>"; } } else { echo "No GET arguments supplied"; } ?> Try this script here http://wuhtzu.dk/random/get.php - it will print all the key-value pairs which is supplied in the URL and http://wuhtzu.dk/random/get.php?1 will print 1 as key and '' as value.... http://wuhtzu.dk/random/get.php?1&2&3 http://wuhtzu.dk/random/get.php?a&b&c http://wuhtzu.dk/random/get.php?a=1&b&c=3 But as jesirose said it is not normal practice to leave out the value of a GET argument ... e.g. index.php?1 Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 27, 2007 Author Share Posted August 27, 2007 Usually it would be something like ?num=1 and they'd use $_GET['num']; I think to get the 1 in the situation you described you'd have to get the url using $_SERVER['REQUEST_URI'] and then a string manipulation function to get the values after the ? I used explode and got the values after the "?"... I call this method junk, and thats my opinion. I always use the other method, and its better off like you guys said to get it off the variable... Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 27, 2007 Author Share Posted August 27, 2007 You can get them by looping over the $_GET array... http://website.com/index.php?1 The above URL has a GET argument called 1 and its value is ''. Try this: <?php if($_GET) { foreach($_GET as $key => $value) { echo "Key: " . $key . "<br>"; echo "Value: " . $value . "<br><br>"; } } else { echo "No GET arguments supplied"; } ?> Try this script here http://wuhtzu.dk/random/get.php - it will print all the key-value pairs which is supplied in the URL and http://wuhtzu.dk/random/get.php?1 will print 1 as key and '' as value.... http://wuhtzu.dk/random/get.php?1&2&3 http://wuhtzu.dk/random/get.php?a&b&c http://wuhtzu.dk/random/get.php?a=1&b&c=3 But as jesirose said it is not normal practice to leave out the value of a GET argument ... e.g. index.php?1 also, thankyou for your explanation, everything is clear now. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 But I can't see why you should/would use that method... It's much easier to use $_GET['something'] Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 28, 2007 Author Share Posted August 28, 2007 Well, as a intermediate coder, I need to know things if I am curious about them... right? So I can go on knowing why its there, and how it works. Quote Link to comment 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.