Say I had the url: http://somedomain.com/file.php?id=x (x as a variable) And I also had http://somedomain.com/file.php The same, but without the ?id=x So what I need to do is, have a code that checks if the ?id=x exists, if it does, execute the given code, if it doesn't ignore. So I pretty much need to know what to use inside the If statement to check if ?id= exists in the url. Thanks!
$url = "http://google.com/exmaple.php?idx=212"; $data = parse_url($url); if($data['query']) { // do stuff.... } PHP:
use: if (isset($id)) { echo "This variable is set!"; } or if (isset($_GET['id'])) { echo "This variable is set!"; } You can download my sample code here http://dalangin.com/category/programming/ It's a basic programming tutorial using PHP/MySQL. Hope it make useful to you. Vote me if you like it!
If register_globals are off then smatts9 code will not work, Have a look at the following code. $id = $_GET['id']; if(isset($id) && $id != "") { // Do your stuff here } PHP:
actually boys I'm not so sure we're working with the current url here as op pm'd saying the code I posted worked for him .....
if (isset($_GET['id'])) { echo "This variable is set!"; } is better, because not all hosts have register globals on (for good reason)
No, I'm not, I'm parsing a url for a query structure, not specifically one website, $url is an example, the user will already have $url set in their script by something else.
Why parse the URL? It makes no sense in that instance. It's much easier to define the query strings with the $_GET variable or the $_SERVER array rather than parsing the URL.
actually we don't know if he is using smarty or not. he asked if how to get the value of id and how if there is no id...so he will be in trouble if he use the $_GET sometimes the variables is in the cache or used sessions so we are not sure of what Crayz want really.