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)
Related
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
}
This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 5 years ago.
I'm looking for a way to turn variables in a URL after the question mark into a simple notation with a slash.
For example: I would like to make it possible to enter this link:
http://localhost/MySite/View?Name=Test
in this form into the browser
http://localhost/MySite/View/Test
The MySite then should recognize "Test" as the Name variable. So basically the two links should give the same result.
How it will be done ?
You should read your request uri and search for the third subdirectory:
$view = $_GET['Name'];
if(!isset($view) && isset($_SERVER["REQUEST_URI"])) {
$uriArray = explode($_SERVER["REQUEST_URI"]);
if(count($uriArray) == 3 && $uriArray[1] == 'View') {
$view = $uriArray[2];
}
}
This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 6 years ago.
I need to redirect dynamic URL like this "https://test.com?user=abc#gmail.com" to "https://test.com"
If I understand correctly, this URL is coming dynamically with a query string. And you want to remove the query string portion and redirect to this modified URL.
Let's assume the dynamic URL is stored in a variable $url.
Try this:
$url = "https://test.com?user=abc#gmail.com";
$modified_url = strstr($url, "?", true); // Remove the query string, which results in https://test.com
header("location:".$modified_url); // Redirect to the modified URL.
You can store the dynamic part in a variable and use the header() function to redirect your user.
$dynamicPart = "someguy#somedomain.com";
header("Location: https://test.com/index.php?username=".$dynamicPart);
exit;
This question already has answers here:
Validating a URL in PHP [duplicate]
(3 answers)
Closed 6 years ago.
How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
I think this is what you're looking for:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
There are actually a few options from my experience. One would be to use the headers() function and then to analyse what information you obtained inside the resulting array.
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
The output should give you all the information you need regarding if the url actually exists.
A simpler solution in my opinion is to just check if the fopen() function actually works on the url!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
You can choose which one servers your needs better.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
# in url getting ignored in php 5.2
I have following link on webpage. Here I am sending return(/profile?id=6#contacts) as url param to return back after operation complete. But it is sending only /profile?id=6 to verify.php script.
http://example.com/verify.php?id=1&sessionId=6&return=/profile.php?id=6#contacts
I know that hash value is not passed to server but I want to know that is there any way to pass compelete return param to server with # value.
Thanks
You need to urlencode() the entire return parameter.