Question:
php script.... how to parse a url?
sharpturds
2007-04-12 08:14:30 UTC
well i want to do somet like you see in google search

for e.g: http://mysite.com/my.php?code=12

now i cant the php script to redirect my site to somet thats given the name 12

like if games.php is assigned to the code 12

then when i pass 12 to the php script he should load games.php


_______________________________

question 2...?

how can i show the number 12 in a text box.....out of the url


http://mysite.com/my.php?code=12



i mean how can i parse the url and get the value out of the url

like code=12

now i want to parse the full url and just get the value of code=
what ever code is equal to he should display it in a text box....
Three answers:
Stefan H
2007-04-13 10:19:05 UTC
Everything after a ? in a URL is called a GET variable.



Given your example of the following URL:

http://mysite.com/my.php?code=12

You can parse a GET variable in PHP by simply calling the following function in you my.php page code:



$my_variable = $_GET['code'];



You can add more than just 1 GET variable. To separate GET variables in the URL, you use & between them. I.e.:

http://mysite.com/my.php?code=12&number=15&word=hello



$my_code = $_GET['code'];

$my_number= $_GET['number'];

$my_word= $_GET['word'];



Now, once you parsed the variable out of the URL, you can use it anywhere in you script (and can echo it out in some HTML code).



-------------------------------------------



In regards to Question 1:



You can use a php header redirect:

header("Location: http://mysite.com/games.php");
anonymous
2016-12-29 09:20:55 UTC
Is the format of the URL the comparable everytime? Is the observe you desire to extract continuously a observe previously a hyphen? you need to sanitise the $url first. Oh and observe that ABC on your occasion would be in $url_array[0] no longer [a million] - a million ought to be XYZ.COM.
xeo_2004
2007-04-12 08:33:55 UTC
to get the url encode:



if i undersand you correctly,



for question 1 use this



$_SERVER['PHP_SELF'];



this will get you to game.php only without the query string.



for question 2



//first get the url using $_SERVER['REQUEST_URI'] to get the whole url



$url = $_SERVER['REQUEST_URI'];



//then explode it into array using "?" as separator to get only the query string



$url_array=explode("?",$url);

print $url_array[1]; // this is the query string code=12



//or you can explode using "=" as separator to get only the value 12



$url_array=explode("=",$url);

print $url_array[1]; // this will return value 12


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...