How can I get the current inputed value of my parameter in url?
for example this is my url
"**www.example.com/register?redirect=**"
when I input a value in the redirect parameter for example is
https://www.google.com
how can I get that value of redirect?
The above comments are correct, but I wanted to take a second to explain why and provide some useful links.
$redirect = (isset($_GET['redirect'])) ? $_GET['redirect'] : false
Through this superglobal, you can access any parameter included in the URL. The values passed to you through this are urlencoded. YOu can read more about it here
What we did in the code above is this:
- Check if the redirect param hasbeen filled (you could aso just use empty() below )
- Assign the redirect value to our $redirect variable if it is set, otherwise assign it false.
Using that, you can redirect the user there if it's set:
if($redirect) {
header("Location: ".$redirect);
}
The downvote above isn't mine, but this isn't such a great question. I understand that you are new to the network, and everyone has to start somewhere. Please try to make sure you include code samples and do a bit of research yourself prior to your next question on SO.
Related
For example, I have a page called profile_page.php. This page is only functional if data is written after the ?u= in the URL, for example, data for Alice's profile page can only be seen when the URL reads http://localhost/profile_page/alice.
Loading http://localhost/profile_page will give me undefined variable errors as most of my variable's are depending on the URL to have a value after the ?u=. For example, the variable $firstname can only be gathered when I get her username in the URL.
In such a case, when http://localhost/profile_page, I would rather have it redirect the user to their own profile_page, but I don't know how I can test the URL and parse it through an if statement.
I understand you can use $u=$_GET['u']; to obtain the current page URL? but I don't think doing this, is the best way to go about it:
$u=$_GET['u'];
if ($u == "http://localhost/profile_page/"){
// redirect to logged in users page code here
}
First, if you are using some parameter for your page to build, the url would looks like httlp://localhost/profile_page.php?firstname=alice&lastname=brown, with $_GET['firstname'] you will get alice in this case. If you want to test if the parameter is set first and redirect to another page if it is not set, you could use
if(!isset($_GET['firstname'])
{
header('Location:redirected_page.php');
}
I am trying to make a simple redirect php plugin, and i cant get to the bottom, i would really appreciate some help.
Inside a folder i have the php script that will handle the redirect, for ex: /redirect/a.php
Scenario 1:
call /redirect/a.php?key=firstkey the redirect to http://www.url1.com
Scenario 2:
call redirect/a.php?key=secondkey then redirect to http://www.url2.com
General rule:
If a.php is called without key, or with wrong key then display Error.
Thank you!
Use global variable $_GET["key"] to get value of "?key=value", then use header() to redirect.
Note that there cannot be any output before calling header(), that applies even for whitespaces (such as space, or tab).
It could look something like this:
// checking whether the key is sent by user who visits the page
if(!isset($_GET["key]))
{
die("Key is required");
}
// checking whether the key is empty
if(empty($key)
{
die("Key shouldn't be empty");
}
if($_GET["key"] == "firstkey")
{
header("location: http://www.url1.com");
}
It would be better to use array() to list keys that should be accepted by script, you could easily look for them by using in_array().
I have several pages which use querystrings to highlight an option in a menu, all the url's on the page have the currant querystring phrased in them so the same menu option will be highlighted on the next page if the user clicks the link.
However the problem arrises when someone visits the page without the querystring included in the url, the menu option isn't highlighted.
What i would like to do is check the URL to see if a querystring is present, if one isnt, create one.
The url's are phrased as such www.mysite.co.uk/Folder1/Folder2/Page.php?id=3
and i would like the default querystring to be ?id=1 if one isn't already present in the url.
Any ideas on how you do this?
And what would happen if a user visits using the URL www.mysite.co.uk/Folder1/Folder2/Page.php?
Would the URL end up as www.mysite.co.uk/Folder1/Folder2/Page.php??id=1
or would it be www.mysite.co.uk/Folder1/Folder2/Page.php?id=1
Thanks,
Maybe there are plenty of ways. You can assign value to $_GET key if one does not exist. Or if you really need to query string, you can renavigate the user to the same page with present querystring.
if (!isset($_GET['id'])) {
header("Location: Page.php?id=1");
exit;
}
It should be before any output in the page. So if user visits Page.php or Page.php? or Page.php?someDifferentParamThanId=10 it will return false on isset($_GET['id']) thus it will redirect to Page.php?id=1
This should work:
if(isset($_GET['id'])){
//It exists
}else{
//It does not, so redirect
header("Location: Page.php?id=1");
}
Do something like:
if(!isset($_GET['id'])){
header('LOCATION:www.mysite.co.uk/Folder1/Folder2/Page.php?id=1'); die();
}
In php, the query string is loaded into $_REQUEST variable. In your case, $_REQUEST['id'] will be equal to 1, 3 or whatever you get in the query string.
For solving the problem when no id is given via GET, I think will be enough to add this line at the beginning of each php page:
<?php
if ( $_REQUEST['id']=='' ) {$_REQUEST['id']=1;}
?>
It is not necessary to change the URL on the fly.
I have a sub-navigation menu on my page that opens up whenever ?n=review is appended to the URL.
I also have languages changing and session variables being set whenever ?language=xx is appended.
However, if I am on a page with the sub-nav open, and I wish to change language from there, the ?n=review is replaced with ?language=xx upon link click, and therfore the sub-nav closes. How would I check to see if a $_GET variable is already set every time the language is changed, and if so, append my additional one to the end?
Once the language has been changed, the new language is set to a session variable so I do not need to worry about keeping it's $_GET variable. I'm wondering if I would need to store the sub-nav variable in the same way...?
Sorry if this question is worded badly, I'm trying to get my head around the logic myself.
If you need to see any code or tidier wording, please let me know before down-voting.
if(empty($_GET['language'])) {
$fragment = '?n=review';
} else {
// if language is set, get the value
$lang = $_GET['language'];
$fragment = "?language=$lang&n=review";
}
// I just made up the /index.php part, replace that with the proper link.
$url = "/index.php$fragment";
Get variables can be chained by using the '&' Character. So you could build your link that way:
echo "./?n=review&language=xx"
If you want to know, if any $_GET parameters have been set, you can simply use PHP's empty() function like this:
if( empty( $_GET ) )
{
//$_GET is empty
}
else
{
//$_GET has something in it
}
I am creating a website and on one particular page, am wanting to send the user back to the previous page. I am fairly new to PHP/HTML and have been using some existing code for ideas and help.
The existing code uses the following method:
if (! empty($HTTP_REFERER))
{
header("Location: $HTTP_REFERER");
} else
{
header("Location: $CFG->wwwroot");
}
However, when I use this code the HTTP_referer is always treated as empty and the user redirected to the root page. Any obvious flaws in this code?
Don't rely on the HTTP Referrer being a valid or even non-empty field. People can choose to not have this set leaving any checks for that variable going to the empty side of the IF-ELSE clause.
You can guard against this by sending along a parameter in either the URL or POST parameters that would hold a value that you can use to redirect the user back to.
You need to use:
$_SERVER['HTTP_REFERER']
isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
If you wanted to send the person back to the previous page and have it work regardless of the referrer being set correctly, you can append a GET parameter to the URL (or POST).. you will need to encode the URL.. Something like
http://www.domain.com.au/script.php?return=http%3a%2f%2fwww.domain.com.au%2fthis-is-where-i-was%2f
You can use PHP's urlencode() function.
Also note that the referer header might be empty or missing anyway, so you shouldn't rely on it at all..
You should use
$_SERVER['HTTP_REFERER']
However look at the register_globals configuration in php.ini, it should be turned off due to security reasons. You can read more on PHP Manual site.