PHP Detect if any URL variables have been set - php

Hey guys, it's kind of hard to explain but basically I want to detect if any variables have been set through the URL. So with my IF statement all of the following should return true:
http://domain.com/index.php?m=100
http://domain.com/index.php?q=harhar
http://domain.com/index.php?variable=poo&crazy=yes
and all the following return false:
http://domain.com/index.php
http://domain.com/test.php
http://domain.com/no_variables.php
Any ideas?

I would test for QUERY_STRING:
if (!empty($_SERVER["QUERY_STRING"]))
should in effect be no different from checking $_GET, though - either way is fine.

if( !empty( $_GET ) ) {
//GET variables have been set
}

(count($_GET) > 0)

If you want to do it with the exception of (a) variable(s), use this if statement before it checks it:
if (!isset($_GET['getvariable'])) {
if (!empty($_SERVER["QUERY_STRING"])) {
echo "do something";
}
}

If you mean taking a string and checking if it has a query string, you can use parse_url.
If you mean checking if the current request has a query string, you can just check the length of $_SERVER['QUERY_STRING'].
If you mean to get a count of the number of variables parsed from the query string, you can do count($_GET);

isset($_GET['m'])
or if anything, I believe count($_GET) might work.

Related

can't get the value from url using php

everyone here ..
now i got an problem , so please help me , because I am new with php code. I want to get value from url but the result is Undefined index
this is url : http://4you4me.com/admin/index_edit_form.php?edit=76
php code :
if(isset($_REQUEST['edit']) === TRUE && ! empty($_REQUEST['edit']))
$con_id_update = mysql_real_escape_string((int)$_REQUEST['edit']);
print_r($con_id_update);
The result is undefined Variable:con_id_update
what is wrong with this script? thank for your answer.
try this :
$con_id_undate ='';// to initialize
if(isset($_GET['edit']) && !empty($_GET['edit']))
$con_id_update = (int)$_GET['edit'];
print_r($con_id_undate);
You shouldn't use $_REQUEST
Also you should use msqli instead of mysql. Since you cast the value to an int you don't need to use mysql_real_escape_string.
mysql_real_escape_string (mysqli_real_escape_string) tends to work only when their is already an open connection
It looks like you're missing the curly braces:
if(isset($_REQUEST['edit']) === TRUE && ! empty($_REQUEST['edit'])) {
$con_id_update = mysql_real_escape_string((int)$_REQUEST['edit']);
print_r($con_id_update );
}
If you don't wrap the curly bracers around the entire statement, only the line after the if statement will be executed.
You should get value from url using $_GET instead of $_REQUEST if you know exactly what you want, good luck!

Use a Variables Value in a Condition

I have a condition stored in a variable $condition = "1==1" and I want to use the value of the variable in a conditional statement.
if($condition) { //$condition should be parsed as 1==1
return true;
}
Is this possible? I've tried using var_export but apparently that doesn't work.
if(var_export($condition)) {
return true;
}
Any help will be greatly appreciated!
After some discussion in the comments above we found out, that the condition itself is not the problem, but how it is passed to the function
conditional_func($var . " == 'test'", $result);
We realized, that it is not required to pass the first parameter as a string, but can use a boolean directly, thus we don't need to evaluate the string at all.
conditional_func($var == 'test', $result);
As a sidenote: #BryanMoyles answer is right regarding the question, but remember, that eval() is evil (you can't think of any pitfall, that may occur) and on the other hand there are only some very less quite esoteric usecases, where you can't use another approach.

PHP to check if a URL contains a query string

This is an easy one. There seem to be plenty of solutions to determine if a URL contains a specific key or value, but strangely I can't find a solution for determining if URL does or does not have a query at all.
Using PHP, I simply want to check to see if the current URL has a query string. For example: http://abc.com/xyz/?key=value VS. http://abc.com/xyz/.
For any URL as a string:
if (parse_url($url, PHP_URL_QUERY))
http://php.net/parse_url
If it's for the URL of the current request, simply:
if ($_GET)
The easiest way is probably to check to see if the $_GET[] contains anything at all. This can be done with the empty() function as follows:
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
echo "Hey! Here are all the variables in the URL!\n";
print_r($_GET);
}
parse_url seems like the logical choice in most cases. However I can't think of a case where '?' in a URL would not denote the start of a query string so for a (very minor) performance increase you could go with
return strpos($url, '?') !== false;
Over 1,000,000 iterations the average time for strpos was about 1.6 seconds vs 1.8 for parse_url. That being said, unless your application is checking millions of URLs for query strings I'd go for parse_url.
Like this:
if (isset($_SERVER['QUERY_STRING'])) {
}

Check whether string is set, even if it's 0

What's the best way of writing:
if(!$this->uri->segment('4') && $this->uri->segment('4') != 0)
This is far too long winded. Just need to check if a string is set, even if it's 0.
isset()
EDIT: nope this is not a var its a method..
Just need to check if a string is set, even if it's 0.
if($this->uri->segment('4') != '')
but i dont think this is what you are trying to do.
it depends on what this method returns and what you try to accomplish.
This is far too long winded. Just need to check if a string is set, even if it's 0.
How about checking for the length of the string?
if (strlen($this->uri->segment('4')) > 0)
EDIT For explicitness, I've added > 0, so it may be a little more descriptive what it is exactly you expect. This isn't necessary, however.
The first clause isn't "correct" anyway, as you've discovered by having to write the second one. You also have to consider FALSE and the empty string (""). Code like if ($var) is lazy and, usually, wrong.
The correct approach for testing a variable is the PHP function isset. However, assuming $this->uri->segment('4') is a function call, the result will always be "set". It can never not be set. So it seems unlikely to be that you can do much here.
What criteria are you really looking for?
Perhaps your function segment returns null? So write if (!is_null($this->uri->segment('4'))).
Or perhaps you're looking for the empty string? So write if ($this->uri->segment('4') != "").
1
if ( strlen($this->uri->segment('4')) )
{}
2
if ( strlen($this->uri->segment('4')) !== "" )
{}

Check query string (PHP)

I use a query string, for example test.php?var=1.
How can I check if a user types anything after that, like another string...
I try to redirect to index.php if any other string (query string) follows my var query string.
Is it possible to check this?
For example:
test.php?var=12134 (This is a good link..)
test.php?a=23&var=123 (this is a bad link, redirect to index..)
test.php?var=123132&a=23 (this is a bad link, redirect to index..)
I'm not sure I fully understand what you want, but if you're not interested in the positioning of the parameters this should work:
if ( isset($_GET['var']) && count($_GET) > 1 ) {
//do something if var and another parameter is given
}
Look in $_SERVER['QUERY_STRING'].
Similar to Tom Haigh’s answer, you could also get the difference of the arguments you expect and those you actually get:
$argKeys = array_keys($_GET);
$additionalArgKeys = array_diff($argKeys, array('var'));
var_dump($additionalArgKeys);
test.php?a=23?var=123 (this is a bad link, redirect to index..)
In this case, you only have one variable sent, named "a" containing the value "a?var=123", therefore it shouldn't be a problem for you.
test.php?var=123132&a=23 (this is a bad link, redirect to index..)
In this case you have two variables sent, ("a" and "var").
In general you can check the $_GET array to see how many variables have been sent and act accordingly, by using count($_GET).
I think you are trying to get rid of unwanted parameters. This is usually done for security reasons.
There won't be a problem, however, if you preinitalize every variable you use and only use variables with $_GET['var'], $_POST['var'] or $_REQUEST['var'].

Categories