This question already has answers here:
How to verify if $_GET exists?
(7 answers)
Closed 5 years ago.
Like the title says, how do I determine which GET value has been passed on within the url?
Take http://www.example.com/view.php?id=20as an example. The current GET value is 'id', with a value of 20. This works great, but if someone plays around and changes the GET value to something other than 'id', I get a lot of PHP errors on the page.
So my goal is to check wether the passed GET value is incorrect, so I can redirect them to another page. How would I go about doing that?
You should validate the URL parameters. In your case, it has to be id and that has to be numeric:
if (isset($_GET['id']) && is_numeric($_GET['id']))
As an advance check, you can validate whether id is integer or not:
if((int)$_GET['id'] == $_GET['id']){
return TRUE;
} else {
return FALSE; // It's a number, but not an integer
}
Related
This question already has answers here:
PHP check if url parameter exists
(6 answers)
PHP to check if a URL contains a query string
(4 answers)
Closed 5 years ago.
I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.
For example:
The url would normally contain the category id:
localhost/myforum/threads.php?categoryid=1
I want it so that when the url is:
localhost/myforum/threads.php
it is to redirect to an error page, and that this piece of code is usable all around the website
The most reliable way is to check if the URL contains a question mark:
if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
// There is a query string (including cases when it's empty)
}
Try:
$gets = parse_url($url));
if($gets['query'] == "")
{
echo "No GET variables";
}
Just:
if (empty(array_diff($_GET, ['']))) {
header("Location: /path/to/error.php");
}
EDIT: Updated to remove empty values
You can use is_set to check if the parameter exists like this,
isset($_GET)
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I am coming up with a way to display user profiles. Right now I am able to insert the user_id into the url when going to a user's profile, but I am unsure of how to get the user_id from the url.
So at the end of the url I have this:
profile?user=41
I just want to be able to get the 41 (please note this number will change per profile) and then set it to a variable.
How would I do this?
<?php
echo $_GET['user'];
?>
all the parameters of the url are stored in the $_GET variable...
The parameter that are in your URL can be retrieved with the
predefined variable:
<?php
//$userId = $_GET["pramName"];
$userId = $_GET["user"];
// or
// using the $_REQUEST variable
?>
This question already has answers here:
Check if $_POST exists
(15 answers)
Closed 6 years ago.
Alright so i receive from $_POST and i need my code do something like this
if ($_POST contains EU-London) {
//do stuff here
}else{
//failed to find EU-London
}
Now i've been told several ways to find a certain phrase in code but the posted data contains:
Array
(
[EU-London] =>
)
How would i check if the EU-London is there? because pregmatch uses strings and im not sure how to grab this using in_array()
You seem to be looking for a key in $_POST:
if (isset($_POST['EU-London'])) {
// Key exists.
}
As correctly commented by Robert, the proper way to check for an existing key would be
if (array_key_exists('EU-London', $_POST)) {
// Key exists.
}
You can check if isset key
if (isset($_POST['EU-London'])) {
//key isset
}
This question already has answers here:
Checking if a variable is an integer in PHP
(14 answers)
Closed 7 years ago.
I am doing a pagination script and I want to give users the ability to control how many results are shown in one page. I am doing this through the use of a GET variable, like this: example.org/articles.php?count=10. Only problem is that the GET variable must be an integer or the code spits out random errors, some of which contains information that the user should not be seeing.
Here is my code:
// Checks if there is a GET variable (this part works fine)
if (isset($_GET["count"])) {
if (!empty($_GET["count"])) {
$page_rows = $_GET["count"];
} else {
$page_rows = $page_rows_default;
}
} else {
$page_rows = $page_rows_default;
}
// checks if the GET variable is an interger
// if not, the offending value is replaced with 0
// (it doesn't work)
if(is_int($page_rows) == false) {
$page_rows = 0;
}
From my experimentation my code can tolerate zeros and negative integers, but fails hard when given something like ?count=asdf. I mostly do not want the user to be able to crash the script by injecting random text into the GET variables. How do I get the script to automatically detect non-integer values so that they can be dealt with instead of simply halting the code?
You can use is_numeric().
For reference http://php.net/manual/en/function.is-numeric.php
is_numeric() can done the trick for you
if(is_numeric($page_rows))
{
//your condition
}
else
{
//another condition
}
This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.