Laravel Check Back to Site - php

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.

Related

Is there a way to get a URL of a launching page in PHP

In PHP 5.6, if I am on a page A, and if page A launch page B, is there a way to get the URL of a page A from page B?
Please note that I do not want to use query parameters to pass the values of a URL A when I launch page B from page A.
If the browser (user agent) supports it, you could use:
$_SERVER['HTTP_REFERER']
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
From: http://php.net/manual/en/reserved.variables.server.php
You can check it using $_SERVER['HTTP_REFERER']. The HTTP referer header will return a string. If you don't know how to work with this you can try:
echo $_SERVER['HTTP_REFERER'];
If your user came by google page, $_SERVER['HTTP_REFERER'] will give you http://www.google.com
And, of course, check the documentation: http://php.net/manual/en/reserved.variables.server.php
If launch means include or require, then you can get URL with this (printing example):
echo $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$_SERVER['HTTP_HOST'] will print base URL and $_SERVER['SCRIPT_NAME'] will print the remaining part (subfolders and file name), like:
http://example/web/pageb.php
I have tested this to make sure it works. But be sure to escape as there could be vulnerabilities here. It also works if $_SERVER['HTTP_REFERER'] is null in your case.

(A|B) testing Google Analytics, remove utm_expid from URL

Im new to this and im trying to rewrite URL so that utm_expid is hidden so if my url is:
http://www.myweb.com/?utm_expid=67183125-2
how would i make it so when user visits
myweb.com
it does not show utm_expid in url
Is this possible using PHP/JS?
NOTE: i cant use RUBY or any other languages except PHP/JS/HTML
There is a way. Just redirect the page to base url once the utm_expid=67183125-2 is got. ie,
if($_GET['utm_expid']) { //header to redirect to myweb.com }
Its a tricky way. Hope you are permitted to use it.
Just start a session and store value in session variable. you can regain it even page is re directed.
ie
<?php
session_start();
if($_GET['utm_expid']) {
$_SESSION['variable_name']=$_GET['utm_expid']
//header to redirect to myweb.com
}
?>
Let me add this Javascript trick that is server agnostic.
if (location.search.indexOf('utm_expid') > -1) {
history.replaceState('page', 'Title', '/')
}
I recommend you to place it at the end of the body.
If you wanted a clean URL (as you do for branding and manual sharing purposes), I'd script it so that you load a full page iFrame which loads the gA test queried URL. That way the user see s the clean URL in the address bar and still see the experiment.
You could use PHP to set up your index page (or any server side, or even client side script).

How to redirect a user from login page to another in php

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'];

What's the easiest way to redirect to the previous page with PHP?

I'm using a form to submit some post information to a PHP script. After the script finishes, I want it to redirect right back to the page the user came from. Right now I'm just using header() with a static URL. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.
Use HTTP_REFERER:
header('Location: ' . $_SERVER['HTTP_REFERER']);
access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.
the way i would do it is use a session variable to store the current page URL everytime it is accessed.
$_SESSION['last_url'] = <get current url>
replace your static url in the header with $_SESSION['last_url']. Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!)
Use REQUEST_URI But watch out for that presiding slash/

Get HTTP Referrer on Redirection

How can you get the HTTP Referrer when redirected from another website, not when they click on a link since it would work for $_SERVER['HTTP_REFERER'], but it doesn't work when a user has been redirected a website and the referrer would be empty.
What will be the method to get the referrer?
How can you get the HTTP Referrer when redirected from another website
You can't. If the redirection takes place under your control, you can add the original referer as a parameter, but if the external redirector doesn't do that, you have no way to get hold of the information.
An example of how I did it. Say we have 3 pages, one calling the next.
page1.com -> page2.com -> page3.com.
in page2.com get the page1.com using:
$referrer = $_SERVER['HTTP_REFERER'];//=page1.com
when redirecting to page3, send $referrer as a GET parameter
page3.com?referrer=$referrer
in page3 read the $referrer from the get.
$initialReferrer = $_GET['referrer'];//=page1.com

Categories