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
Related
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.
How to get the request page name or uri from which the source page is loaded.
Explanation below:
index.php
<?php
header('Location:target.php');
?>
target.php
<?php
// How to check here if the redirection has happened from index.php
?>
you can use the code below
echo $_SERVER["HTTP_REFERER"];
but to make sure you can get really the exact origin of your page you can store the current url to a session before doing a redirection and read it to the next page.
in the origin page
$_SESSION['origin'] = $_SERVER['REQUEST_URI'];
then in next page
echo $_SESSION['origin'];
you can use $_SERVER["HTTP_REFERER"] but it can't be trusted. Some browsers will have the correct value other will have null value.
Here is what php manual says about it:
'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.
In short, I don't think you can get a reliable solution for what you want.
Ok, so I'm trying to do a simple action where if a user was already viewing our site, we'll display a certain message, otherwise, we want to show a message for first time visitors (or just those without our URL as their referrer)
Else statement only shows as blank regardless how I seem to spin this.
Ideas?
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
if (strpos($referer, "http://www.example.com/") === 0) {
echo "Match Okay";
} else {
echo "No Match";
}
}
?>
Your code seem ok.
Quoted from the php docs:
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.
Maybe your browser belongs to the category "Not all user agents will set this". Try your code with another browser.
For a more reliable way do to that, consider using cookies! Simply check for a cookie value. If it is not present, it's a first time visit (then set the value). If it is present, then it's at least the second page loaded.
The advantage of cookies is that you can track user if they come back again (by setting a persistence time for the cookie)
You should use cookies
SOURCE: http://php.about.com/od/advancedphp/qt/php_cookie.htm
I want to make a count of visits to my website from referal websites. I know there are many programs such as Google analytics but there will show you that my taffic is coming from www.facebook.com for example. I want to check if the traffic is coming from some specific urls that I specify such as www.facebook.com/myfanpage.
Befor I think about php I tried several methods with javascript that they did not seem to function the way I wanted to. For my search for php I only found this function. Any Ideas ?
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_REFERER'] Will do exactly what you need.
if (strstr($_SERVER['HTTP_REFERER'], 'facebook.com') !== false) {
// Facebook brought me to this page.
}
elseif (strstr($_SERVER['HTTP_REFERER'], 'google.com') !== false ) {
// Google brought me to this page.
}
Sorry, I know this is 6 months late but surely if the url was http://mydomain.com/?p=facebook.com then this would also be true? a better way would be to explode the referrers url based on / then extract the 4th section i.e.
$refererUrl = $_SERVER['HTTP_REFERER'];
$Exploded_URL = explode("/",$refererUrl);
$urlToCheck = $Exploded_URL[3].'.'.$Exploded_URL[4];
if($urlToCheck == 'facebook.com'){
/* From Facebook */
} elseif ($urlToCheck == 'google.com'){
/* From Google */
}
$_SERVER['HTTP_REFERER'] should contain the URL that the user is coming from to get to your page. It's not a function. It's simply a value. So you can use it for this purpose.
Do note, however, that the value is easily spoofed. (It's taken from the HTTP request header, and the user can send whatever they want.) It should be acceptably reliable if you're just collecting stats for your own interest or whatever. But if you're trying to use it to secure the page (e.g., only show certain content if the visitor came from a certain URL), forget it.
You will be able to check only if the HTTP Request has referer which is actually accessible in PHP using HTTP_REFERER. So its solely responsible from the referring website.
Get original URL referer with PHP?
The above post also will help you.
I have a PHP page on a website that I'd like to be accessible only from another page on that website.
If a user clicks a link to the page, or types the page's address, and does not come from the site's domain, I'd like the page to redirect the user to the index.
Any ideas?
What you could do is use sessions.
make the index set a variable
$_SESSION['visitedIndex'] = TRUE;
and testing for it in the other pages:
if(!$_SESSION['visitedIndex']) {
header('location: ....');
}
make sure you do this before the first echo.
You could also create an internal service using a $hash = timestamp + internal secret key or your paricular rule.
First page has a link
http://www.samesite.com/page_2.php?param=hash
Second page decodes the hash and check the timestamp against a given interval. Otherwise it refuses the display.
As only you know the internal key is impossible to fake.
Check 'Referer' field?
It's easily hackable, tho. The more reliable way is to check if the used had no active session (if your site assigns them to visitors).
Use the referer fo this:
if ($_SERVER['HTTP_REFERER'] != "...") {
header("LOCATION: othersite");
}