My problem is that, I have a table with values, when I pulse on one of the cells redirect to a cell details page, in the url I pass the id: "http://localhost/example?id=123456". when I back to the previous page with a cancel or save button does redirect to the previous page and get in id with $_SERVER['HTTP_REFERER'] but when you pulse the back button in the browser I don’t know how to get that url.
You can use the session system of PHP:
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
To redirect to the last page you can use the header function.
header("Location: ".$_SESSION['page']);
exit();
On the server-side using $_SERVER['HTTP_REFERER'] is enough and you don;t need to store the value of $_SERVER['HTTP_REFERER'] on $_SESSION too. just directly check the $_SERVER['HTTP_REFERER'] val.
For the client-side, you can use document.referrer in JS. Also, it can be helpful for you to know these functions in JavaScript:
history.back(); //Go to the previous page
history.forward(); //Go to the next page in the stack
history.go(index); //Where index could be 1, -1, 56, etc.
Notice: If you have some routing system that handles the URL dynamically on the same path ($_POST or ...) you may need a more complex solution based on your framework.
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');
}
This is the scenario: a user land on a page which redirects him at certain conditions with the following php lines
header("Location: /access/details/index.php");
die();
The problem is that the page /access/details/index.php should receive the referral URL correctly. I cannot insert an input tag because of the PHP redirect. What is the simpliest way to pass the URL to the redirect destination page, possibly without using other languages such javascript?
There is no way to tell the browser what URL to use for the referrer. Instead, you can pass the referrer as a get parameter in the redirect.
header("Location: /access/details/index.php?referrer=" . urlencode($_SERVER['HTTP_REFERER']));
Retrieve the previous referrer on your /access/details/index.php script by accessing the $_GET super global
$referrer = $_GET['referrer'];
Another option would be to skip the redirect altogether and do a forward. This keeps the current referrer intact.
include("/access/details/index.php");
die();
I want to check redirect to another link from our webpage if user clicking on back from browser I must be alert for user such as 'Backword Forbidden ...'
I'm using this code and that not working for me:
$referer = Request::header('referer');
or how to check witch URL user backword to our site?
If you want to get the Referer URL, you can use either Request::header('referer') or native $_SERVER["HTTP_REFERER"]. But there are (at least) 2 problems with that:
It can be spoofed, empty etc.
It will only work if the person got to your page through a link. It won't work when pressing the browser's back button or backspace.
The function you're looking for is Request::server() which functions just like the $_SERVER super global, so to get the page referer you'd do the following.
$referer = Request::server('HTTP_REFERER');
Using Request::header('refer') will only work for POST requests.
GET requests are the one your're looking for.
You can use Request::segment(1) or Request::segment(2), depends on the exact URL you're using.
Let's say a user has bookmarked "http://www.example.com/login#/settings". If that user try to access this page when he is logged out, firstly i want him to redirect to login page and then to the bookmarked page using this method
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
NOTE:
I'm using MVC architecture
Are there any method rather than HTTP_REFERER?
When user enter http://www.example.com/login#/settings ,i want to read whole url including # anchor in my controller file and then only i can set url to
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
so how do i do it??
You cannot read the part after the # from within PHP. You need to use JavaScript for that. For example, you can use
window.location.hash
To locate the hash-part, if any (it will be '' if no hash, or '#something' if there is one). You can then send this to the controller as a hidden field inside the request.
Depends on how you want to present it to your user. For a simple redirection, use header to send the HTTP Location Header
header("Location: http://www.google.com/");
If you want to give your user some time to read a short message before redirecting them, then you can use header to send HTTP Refresh Header
header( "Refresh: 5; url=newpage.php" );
Edit: In order to capture the anchor, you will need to use JavaScript. That information is not available to PHP. In that case, if you use JavaScript to capture the anchor, you might as well write your redirection in JavaScript.
Edit 2: Perhaps the other option is, when you are passing the continue to your program, also send the anchor as another GET variable. So your URI might look like this:
http://www.example.com/authenticate/login?continue=http://www.example.com/login&anchor=settings#/settings
Then use $_GET['anchor'] and concatenate it to the value of $_GET['continue'] with a #.
$uri = $_GET['continue'] . "#" . $_GET['anchor'];
Here is the scenario:
Visitor of Page1.php is being redirected with JavaScript to Page2.php
Is there a way to know that visitor which lands on Page2.php is a redirected visitor by monitoring Page2.php if I don't use any sessions and variables at all in any language?
Without Doing/Using:
URL Manipulation
Cookie
Session
Any kind of Variables
Absolutely no changes to Page1.php
I'm asking this because I don't want other sites to detect that I have redirected users to their website.
I just want to know the possibility.
Just set a flag in the query string when you redirect (append the query string to your redirect location):
Page2.php?redirect=1
Or if you need the referring page:
Page2.php?referer=Page1.php
Then check with $_GET['referer']
You might be able to read the $_SERVER['HTTP_REFERER'], but I personally tend to avoid it because it doesn't always contain what you think it should.
If you don't want to use server-side languages, your only alternative is JavaScript. You could redirect to Page2.php?redirected=true and use the following code to GET the redirected variable on Page2.php.
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
if($_GET['redirected']){
// Redirected from Page1.php!
}
Source: how to get GET and POST variables with JQuery?
Set a javascript cookie on the initial page when you do the redirect.
On the new page, check to see if the cookie is set, then delete it.