Hide referring url parameters - php

When I call the script using /landing.php?source=param I want the script to do a redirect to a URL on another domain, but I don't want the people on the other domain to see the source=param parameter in their analytics or server logs, I don't mind them being able to see the /landing.php URL.
Any ideas on a solution?

A solution is to redirect to an URL on your server (possibly the same page) that doesnt contain the query string first, and then send the user to your off-site destination. You can pass the needed parameter/url on your server with a $_SESSION or $_POST
/landing.php?source=param > /redirect.php > www.offsite.com

In your landing.php, check for the source variable and then redirect it to landing.php or another page without the GET variable e.g. :
if(!empty($_GET['source'])){
// Save the source into a COOKIE / SESSION
// Then redirect to another page to strip out the GET variable.
redirect('landing.php');
}
Then either in landing.php or another file, just redirect to the offsite URL. You'll have the source in a COOKIE / SESSION to use if you need to do anything else with it.

Related

check if page was redirected or opened directly in php cross-domain

How can I check if I was redirected from another domain to page or opened directly in right domain?
Thanks for answer!
I assume from the tags, which you assigned that you own an server, running PHP and want to know whether the users, visiting your page are comming from a page belonging to your domain or from somewhere else.
This is normally stored in the referer header of an HTTP request.
Try accessing it in PHP with $_SERVER['HTTP_REFERER']
The variable should contain the whole path of the source page and you can extract the domain/hostname using parse_url()
Complete example:
<?php
$sourcehost = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
?>
I tested it but ufortunately, after redirect 301 there is no data stored in the $_SERVER['HTTP_REFERER'] variable.

Send a message with PHP header location redirect

Is it possible to include some message in a PHP header:
header("Location: http://somesite.com");
header("Message: hello");
then on site.com:
$message = some_function(); // "hello"
I am currently using a $_GET parameter in the URL, but looking for an alternative, maybe sending a $_POST?
I'm trying to not use $_GET, or use cookies (I know, those are the best ways..)
It sounds like you are wanting to send some extra data to the page you are redirecting to. No, this isn't possible outside of the query string. You should understand what is happening here.
When you send a 302 or 301 status code along with a Location: header, the browser sees this and then makes a separate request to the URL specified by the Location: header. The server isn't sending anything to that page. It's almost as if the user simply typed in that new URL in their browser.
I say almost because in some circumstances, there is a referrer set by the browser. This isn't guaranteed though.
What you can do is send some sort of token that contains more information. Perhaps your page saves off a message in a database or something, and then you pass the ID in the query string of the URL you're redirecting to.
Also, if you set session/cookie data and you're redirecting to something on the same domain, you can read that information on the page the user eventually lands on.
In addition to what Brad suggested, you can also send some info using # in the url without affecting the query string and then capture it with js.
header("Location: http://somesite.com#success");
in js:
if(window.location.href.indexOf('#success')>0) {
alert("operation successfully completed");
}

How to get original URL form 301 redirection

I want to get original URL (user entered URL) from 301 redirected URL.
Ex:
www.mydomain.com/about-1/
www.mydomain.com/about-2/
www.mydomain.com/about-3/
All above URLs want to redirect www.mydomain.com/about/. So I did it using simple .htaccess redirect. My problem is how to find from where user came (which original URL).
I've tried $_SERVER['HTTP_REFERER'] but it didn't work.
Passing variable like www.mydomain.com/about-1/?val=1 may a easy solution but it is not possible in this situation.
the HTTP_REFERER in _SERVER tells you where it is from. What I do sometimes is append a parameter to the url (either with a script.php?param=source or with a path script.php/path) to help me make the processing easier. These parameters can be added transparently to the script with an htaccess rule)
The easy way to do this is to take the url of the referrer to www.mydomain.com/about/ URL as a variable.
<?php header("Location: http://www.mydomain.com/about?referrer=http://www.mydomain.com/about-1" ,TRUE,301); ?>
Then you always have the referrer with the $_GET variable.
You can do it using PHP. Here is the link to get code to do it.
http://www.phpjunkyard.com/tutorials/php-redirect.php

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

Categories