I need a code to display referrer URL (full URL if possible, like example.com/1234). if full URL is not possible, then at least the domain is fine. Also, I want to see if the referrer is NOT coming from somewhere for example: if referer is NOT example2.com then //do action
I have a code but it's not working properly. If you can help, that would be great. Thanks
<?php
// Check if Referral URL exists
if (isset($_SERVER['HTTP_REFERER'])) {
// Store Referral URL in a variable
$refURL = $_SERVER['HTTP_REFERER'];
// Display the Referral URL on web page
echo $refURL;
} else {
if (( $refURL == !'https://example2.com' ) ) {
// sorry, you didnt come from example2.com, you NEED to come from example2.com}
?>
This doesnt seem to work. Also, http refer only displays domain of referrer without path like example.com/1234/321. If its possible to get the full URL please share, otherwise i can do without it.
Related
Let say I have redirect rule on url example.com which redirect to newurl.net.
How can I get original url on newurl.net in order to be used and handled with some details or just to be printed on new destination.
Thank anyone for help.
If it's not external You can use
$_SERVER['HTTP_REFERER']
And it will give you the referer which is what you want.
See Acquiring referral after a PHP redirect for more informations.
But if you are trying to get to another website, the browser will overwrite the headers so you need to do it some other way.
You can store it in session.
//Save it in your first website
$_SESSION['REFERER'] = $_SERVER['HTTP_REFERER'];
And then in the other website :
//Use it in the other
$referer = '';
if (isset($_SESSION['REFERER'])) {
$referer = $_SESSION['REFERER'];
unset($_SESSION['REFERER']);
}
You could also pass the referer as a parameter:
header('Location: http://newurl.net?original_referer=' .$_SERVER['HTTP_REFERER']);
I have the following code to determine the URL used to load a page, it works in all browsers except IE.
Is this a known issue?
if(isset($_SERVER['HTTP_REFERER']))
{
//correct domain:
$domain=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false )
{
}
else
{
echo $domain['host'];
}
}
Is there a different way to get the URL that the user is using? Essentially I need to know what URL the user has entered to determine what to display on the screen.
Is this a known issue?
Yes:
'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.
Also the above differs from what you want:
Is there a different way to get the URL that the user is using? Essentially I need to know what URL the user has entered to determine
what to display on the screen.
REQUEST_URI is what you are looking for:
'REQUEST_URI'
The URI which was given in order to access this page;
Source: http://php.net/manual/en/reserved.variables.server.php
Also see: Get the full URL in PHP
I am trying to get the URL of the site which referred the user to the site with the script on it.
Here is the script I am using:
if (isset($_SERVER['HTTP_REFERER'])) {
$ref_url = $_SERVER['HTTP_REFERER']; //get referrer
}else{
$ref_url = 'none'; // show failure message
}
echo $ref_url;
The issue I am having is that no matter what site I come to the main site from, I always get 'none' echoed. What is causing this and how can I make the script actually get the referrers URL?
I have made a facebook application
I have uploaded all my code on my server, so facebook can retrieve it from there.
All my code is in HTML, php and javascript.
When the user visits www.mywebsite.com/facebook/app they will go to my index.php file.
But if the user types in www.mywebsite.com/facebook/app/pictures/picture.jpg he will see the picture.
Now I want to make sure, this content only can be access through facebook.
So I want to redirect everybody who tries to enter www.mywebsite.com/facebook/app/..../ to my facebook application www.facebook.com/myapp
Is there any way to do this?
Thank you
While it is very unreliable you could use the $_SERVER['HTTP_REFERER'] variable in php to see if the user typed in the url directly in their browser. It will be empty if the user has typed in the url directly, but i believe it should be set if your page is embedded through facebook.
see http://www.electrictoolbox.com/php-http-referer-variable/ for more info
How I fixed it
// Saves the original URL
$Origin_URL = $_SERVER['HTTP_REFERER'];
// The original URL must also contain the word facebook
$face = "facebook";
// Checks if the URL contains 'facebook'
$contains = strpos($Origin_URL,$face);
// If the original url is empty or doesn't contain facebook
// the user will be redirected to the facebook site
if(Origin_URL == "" || $contains === false)
header("Location: www.facebook.com/website");
This is not bulletproof, so try toy with it a little :)
Let say we've the following
Objective : User will post certain exact URL $refere to lock viewing text content and only be allowed for view if the viwer is coming from the same exact URL $refere.
$refere = "http://www.site_site.com"; // User will post it
$r = $_SERVER['HTTP_REFERER']; // To get real referral
and i want to do the following
<?PHP
if(stripos($r, $refere) == false){
echo "Wrong";
} else { ?>
echo "Go";
}
?>
It always gives me $r = $_SERVER['HTTP_REFERER']; blank ! so does it deprecated on any PHP version 4 or 5 whatever !
Also
what is the user posted $refere like https:// or missed www. or only posted site_site.com while the $r = $_SERVER['HTTP_REFERER']; showing www.site_site.com
so can anyone help me to adjust this code to be working fine no matter the user posted the $refere link fully or only site_site.com.
The $_SERVER['REFERER'] variable will only be set when you click a link to your page from another page and if the browser (or an eventual proxy or firewall you're on) isn't removing the referer header.
To your second question: do some string comparisons. The functions strpos() and substr() will be of great help.