I have a tracking script that I use to save analytic data to our company database. We have quite a few websites (around 2000 domains) and PPC campaigns and the script I'm using works just fine.
I know that the $_SERVER['HTTP_REFERER'] is not 100% reliable and can be either empty or spoofed, whatever, that is a small minority of the leads we have coming in (I take this into account in my tracking script).
The problem is that although my $_SERVER['HTTP_REFERER'] var always comes back empty if I echo it out on the affected page, it is passed to the tracking script (via a $_SESSION var) as the current page URL. It's as if (note the 'as if', I know this is not the case) PHP is substituting $_SERVER['REQUEST_URI'] for $_SERVER['HTTP_REFERER'].
This is from the landing page:
$_SESSION['keywords'] = $_SERVER['HTTP_REFERER'];
require_once 'tracking.php';
$raw_query = $_SESSION['keywords'];
$key_browser = getKeywords($raw_query);
$keywords = $key_browser['keywords'];
$referer = $key_browser['referer'];
$user_agent = getBrowserOs($_SERVER['HTTP_USER_AGENT']);
$br = $user_agent['browser'];
$os = $user_agent['os'];
The tracking script is inconsequential because the variables I pass it are not altered.
if you load your script with HTML tag like <script src="mytracking.php"></script>, referer will be the same as request_uri because request_uri is the one that requests the script.
The only script that gets the referer from which user came if link is clicked is the script that responds to the request from the browser. All resources, loaded via HTML tags will have the current page as referer. Which, by the way, is often used as a protection against hot-linking of images and other resources.
Well I've found no proper solution so I've opted to use a hidden field whose value is populated with javascript's document.referrer property and simply passed that to the tracking script. Definitely works although I'm not too pleased that I couldn't find a better solution.
Related
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.
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).
I want to get the URL using (preferably) PHP or JavaScript. The page was opened using an anchor name (e.g. index.php#aboutme). When I use
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
it returns http://afterimagedesign.tk/index.php without the #home on the end. How can I get this?
PHP cannot ever get this hashtag (that's what it's called), because the browser never sends it to the server in any form.
JavaScript can access it with window.location.hash, but that's client-side.
What are the difference between server-side and client-side programming?
<?
echo parse_url("http://localhost/index.php#aboutme",PHP_URL_FRAGMENT);
?>
Output: aboutme
or with JS
window.location.hash
The only way to access this data with PHP would be to send it to PHP via an AJAX call when the page loads. As the other responses have said, the hashtag is never sent to PHP inside the original request. So you would have to send it afterwards.
If this is information you need, you would have to change your application to not use hashtags and instead append this data to the query string (ie index.php?home rather then index.php#home and manually scroll the page using javascript when it loads based on this.
At my work I often need to figure out where our traffic comes from. We buy google ads and that traffic gets identified by a query string in the url. (mywebsite.com/?x="google_ad_group_4").
On every page I include some sessions stuff that sets $_SESSION['x'] to $_GET['x'] if $_GET['x'] is there. If there is no $_GET['x'] I go through some other options to see where they came from and set that in $_SESSION['x']:
$refurl = parse_url($_SERVER['HTTP_REFERER']);
$query = $refurl['query'];
parse_str($query, $result);
if (isset($result['q'])&& strstr($_SERVER['HTTP_REFERER'],'google')) {
$_SESSION['x'] = 'G-'.str_replace('\\"',"X",$result['q']);
}elseif (isset($result['p'])&& strstr($_SERVER['HTTP_REFERER'],'yahoo')) {
$_SESSION['x'] = 'Y-'.$result['p'];
//took out bing, aol, ask etc in the name of brevity
}else{
if ($refurl['host']){
$_SESSION['x'] = $_SESSION['x'].'_ref-'.$refurl['host'];
}
}
This way I can append the search query that brought the user to the site and what search engine they used. I log the incoming $_SESSION['x']'s.
Many users are coming in with $_SESSION['x']'s of "_ref-mywebsite.com" which doesn't make sense, if they were coming from my own domain, they'd have already had a $_SESSION['x'] set on whatever page they'd been on. Is this because they have their browser's security turned up high or something?
Am I missing something obvious? Is there a smarter way to do this?
You can get the referrer like this
echo $_SERVER['HTTP_REFERER'];
But as mentioned in comment, it can easily be manipulated.
Unless the client (the browser) passes you the "HTTP_REFERER" in the heading, you won't get it. And that depends on the site they come from.
I don't know what your workflow is like, but one thing you can do is get it with JavaScript and pass it to your PHP script. Hope this helps.
I think that a possible scenario is:
A new visitor comes to the website with normal referrer;
He closes his browser(this clears his session cookie) with the website's tab opened;
Reopens the browser with the website restored in old tab;
Clicks on any link on the page and gets to another page with referrer from same domain and clean session.
I use;
$referrer = $_GET['_url'];
If I echo $referrer; it will display correctly.
If I use $referrer within a $_POST, it is empty. I think due to $referrer being assigned to $_GET.
How can I extract the value of $referrer into another variable so it is no longer assigned to the $_GET?
I hope that makes sense..
$_POST will only contain data IF you send that from form.
So, your code is basically right. Because you use referrer from within your URL.
If you really want to have $referer from $_POST, you will have to code something like this:
<form method="post" action="somewhere.php">
<input type="hidden" name="_url" value="{place the referrer here}" />
</form>
Or, like #Michael Gillette answer, you can change that with $_REQUEST.
hope that makes sense..
sorry, but it doesn't :)
$referrer become distinct variable with no relation to $_GET['_url']. It already contains value extracted from $_GET
there are not a single reason for $_GET sourced variables to conflict with $_POST.
Your problem is somewhere else.
It seems you're just trying to access variable that doesn't exist. Because every variable dies along with whole PHP after it's execution.
PHP scripts execution is atomic. It's not like a desktop application constantly running in your browser, and not even a demon with persistent connection to your desktop application. It's more like a command line utility - doing it's job and exits. It runs discrete:
a browser makes a call
PHP wakes up, creates an HTML page, sends it to the browser and dies
Browser renders that HTML and shows it to the user.
User clicks a link
a browser makes a call
another PHP instance, knowing nothing of the previous call, wakes up and so on
So, if you set your $referrer in one instance and trying to access it in another, it will fail. You have to re-sent it's value with next call
Use the clone keyword, as seen in the examples in this article:
http://php.net/manual/en/language.references.php
could you post an example of what you might like to do?
$referrer = $_REQUEST['_url'];
will return true on both GET and POST requests