I have a small script that redirects users to main site if they come from a banner on my/other remote sites.
<?
.
..
...
....
header("location:$golink");
?>
But google analytics will not show the referrer site (where the script is working) instead it shows the url where the banner is clicked. Obviously I can not keep a track of all sites where banner appears and dont want to. I want the refferer to be the site where the script is working. How do I have to use the $_SERVER['HTTP_REFERER']; in order to do this ?
GA has a method that will let you to override the default referring URL (document.referrer) with a specified value.
So if you want to keep the redirect server-side, you can append the referring URL as a query string param in your header() call, and then look for it on the target page and specify it as the referring URL.
I don't know how you are building your $golink variable, but basically you would add something along the lines of:
$golink .= "?ref=" . $_SERVER['HTTP_REFERER'];
Use a & instead of ? if there are already URL params, and the code above assumes using ref as the URL param, so use whatever var you want.
Then on your target pages, before your _trackPageview call, you would add
_gaq.push(['_setReferrerOverride', ref]);
ref would be a javascript variable with the value of the ref=xxx query string param. For some weird reason Javascript does not have a native way to grab URL param values, nor does GA provide an (exposed) solution. If you already have a solution on your pages for grabbing URL params (like something from a framework or a function you've already made) then use that. Otherwise it's pretty easy to find a javascript function that will do it for you.
There are a couple benefits to doing it this way:
You don't have to worry about the visitor seeing an interstitial page.
You don't have to worry about GA not getting a chance to fully load before redirect
You can see the referrers tied directly to your landing pages, because with the interstitial page, you will always see that interstitial page as the referrer, and will have to look at referring url reports for the interstitial page.
Yes, G.A is blind to this kind of server-side stuff. And their PHP Api is not helpful either.
However, you could have a short redirection page, holding the GA tag inside like this :
<html>
<head>
<title>A web page that points a browser to a different page after 2 seconds</title>
<meta http-equiv="refresh" content="2; URL=<?php echo $golink; ?>">
<meta name="keywords" content="automatic redirection">
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
</head>
<body>
If your browser doesn't automatically go there within a few seconds,
you may want to go to
the destination
manually.
</body>
</html>
Notice the $golink variable in the meta tag.
If you use this, do not forget to replace UA-XXXXX-X by your real account number.
Credits : optimized GA tag goes to Mathias Bynens
[EDIT : javascript only version]
<html>
<head>
<title>Redirecting you...</title>
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
<script>
<!--
if (window.addEventListener)
window.addEventListener('load', function() { window.location="<?php echo $golink; ?>"; }, false);
else
window.attachEvent('onload', function() { window.location="<?php echo $golink; ?>"; });
// -->
</script>
</head>
<body>
</body>
</html>
Related
I'm trying to do a redirect with body in Laravel. I've tried this:
return Redirect::to($log_in_url)->with('body','<html><head><title>Redirecting to your page</title></head><body>If you are not redirected within 5 seconds, please cl ick here.</body></html>');
I look in the network tab, I don't really see anything.
The question is that how would one make a delayed redirection by showing an HTML waiting page before the actual redirection happens?
You're making a handful of false assumptions:
The with method puts the thing into the session so that you can access it after the redirection. A common usecase is to set messages and then redirect the user.
Don't expect magic by just setting the thing body.
There's no such a standardized redirection called "redirect with body" as you stated. If you need such a thing, you have to implement it.
I assume you're having one of those vBulletin-like redirect styles in mind. To implement it in Laravel context, you gonna need a mediatory view to do a clientside redirect for you after a set amount of delay. Let's name it redirect.blade.php:
<!doctype html>
<html lang="en">
<head>
<title>Redirecting...</title>
<script>
window.setTimeout(function () {
window.location = "{{ $url }}";
}, 5000);
</script>
</head>
<body>
If you are not redirected within 5 seconds,
please click here.
</body>
</html>
With this in place, your controller will pass a $url to this mediatory view and let it be rendered to do the clientside redirection:
# Controller method
return view('redirect', ['url' => $log_in_url])
This style of redirection won't be working if JavaScript is disabled and that's why they put a link into the page content and warn the user about it.
Some take a hybrid approach:
<noscript>
<meta http-equiv="refresh" content="5;url={{ $url }}" />
</noscript>
The reason that they don't go with the refresh header/meta tag in the first place is that it's not specified in the HTTP standard. Read more.
I strongly suggest that you look into alternatives. This is so 1990 and not user-friendly at all.
As a visitor, if I deal with a website that makes me wait for 5 godddamn seconds, I'd just leave. That's why people used to make browser extensions to workaround the vBulletin's login screen waiting time!
Embrace simplicity and just do a regular HTTP redirect. It's best for all humanity.
It's not a task for Laravel. You can just return page with meta, or using javascript.
// Controller
return view('redirect');
// View redirect.blade (Regular html page with additional meta tag)
<meta http-equiv="refresh" content="5;url=http://www.google.com/" />
I apologize if this seems stupid or redundant, I've search and read related pages with little understanding.
I use this function to call my chat widget to each page. (In case I would like to switch chat server.)
<?PHP include "newchat.php"; ?>
I would like to refresh newchat.php at an interval of 20 minutes. (To prevent chat time out.)
I use this code on newchat.php, which results in the entire main page to refresh. (ie. index.php)
<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
</head>
<body onload="JavaScript:timedRefresh(10000);">
*chat script here*
I think I may need to put script/ajax on each template page, which tells browser to refresh only that element, however I do not understand this code and am not sure if it applies.
Thank you for reading and help you may provide.
Try putting your newchat.php file into a iFrame on your pages instead of directly including it. ie:
<iframe src="newchat.php" id="chatFrame" frameborder="0" width="YOUR-WIDTH" height="YOUR-HEIGHT">
Then you can refresh from with-in the newchat.php file. Or if you want control over it from each page you can use the iframes id to control a refresh from the parent:
document.getElementById('chatFrame').contentDocument.location.reload(true);
My CMS links to other sites for convenience and I'd like to hide the referer so that other sites don't see the directory and the query string of my CMS. I now have the CMS linking to a PHP file elswhere on my server which in turn redirects to the link via header() but the referer is still from my CMS, not from the linking PHP. Furthermore...
header("Referer: nowhere");
header("Location: $_REQUEST[urltolinkto]");
... doesn't appear to change anything. No matter what I put as referer, it's always the one from my CMS where the user actually clicked on the link.
Can the referer be changed (to the linking PHP), or do I have to use javascript or meta refresh?
The Referer header is something the browser sends to the Server. You are changing the respose from the server to the browser, so that will not work this way (unlike the Cookie header). As far as I know you have no server-side control of the browser's behavior on sending the Referer.
The browser does get to choose what referrer to send, but there are ways around it.
HTML5 added meta referrer, most modern browsers will respect it. Just add
<meta name="referrer" content="no-referrer">
to your site's head.
There's also redirection services and other hacks to hide the ref (https redirects, iframe tricks and others).
A good solution is to simply use the classic <META HTTP-EQUIV="REFRESH" CONTENT="0; URL=http://www.example.com/">.
In fact, Google Analytics has a help page specifically for this question with users who ask about web-tracking not working on redirects, here: Support.Google.com -> Redirects: Place the tag on redirecting pages. They explain the problem quite well:
If your site uses redirects, the redirecting page becomes the landing page's referrer. For example, if you've changed your site so that index.html now redirects to home.html, then index.html becomes the referrer for home.html....
For this reason, you should place the Analytics tag on the redirecting page as well as on the landing page. This way, the redirecting page will capture the actual referrer information for your reports.
So, just swap out header("Location...") with a massive series of print statements. This feels so inelegant. But it works.
Note: I'm also throwing in a canonical attribute so browsers understand the point of the redirect more clearly.
<?php
$redirect_url = 'https://www.example.com';
$google_analytics_configgtag = '12345, this is your api key';
print('<!DOCTYPE HTML><HTML><HEAD>');
print('<META HTTP-EQUIV="REFRESH" CONTENT="0; URL=' . $redirect_url . '"/>');
print('<LINK REL="CANONICAL" HREF="' . $redirect_url . '"/>');
if($google_analytics_configgtag) {
?>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php print($google_analytics->configgtag); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', '<?php print($google_analytics_configgtag); ?>');
</script>
<?php
}
print('</HEAD>');
print('<BODY></BODY></HTML>');
?>
You cannot set Referer header manually but you can use location.href to set the referer header to the link used in href but it will cause reloading of the page.
You cannot really change the referer from server-side as it is provided by the browser to the server.
But you can use a service like href.li, just use
https://href.li/?http://<your-url>
Note: http:// after ? is important or it will not redirect.
Hi I'm looking to implement a similar feature to that seen on deviantART if a person clicks onto an external link: example: http://www.deviantart.com/users/outgoing?http://driz.co.uk/
What would be the best way of implementing such a feature? From the looks of it on dA they just edit all the external links to have the http://www.deviantart.com/users/outgoing? prefix and then show that page which allows them to visit the site or return. So perhaps I'd need someway of implementing a way to check which links are NOT on the current domain and then prefix them with a special url to show a stepping stone page.
Or perhaps just appending the rel="external" to the links and then somehow for all links that have the rel tag of external go to page like that or perhaps show a dialog showing the same message.
Cheers
For the user input html use a regular expression:
<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]
where $1 will be the href, now you can test against:
(\w:\/{2})?([\w\.]+)
if $2 is not one of your domains you need to rewrite the href, so locate the position of $1 in the first regexp (strpos in php) and add your mask url
http://yourdomain.com/maskurl?
The query string as is will be the url you want to redirect to
By the other side if you are printing the links with a given url you need to pass only the second one.
The first regexp from here: http://www.onaje.net/content/working-regular-expressions-href-url-extractor and the other was written by me, but tested none.
The detection part: a link only needs this kind of decoration if it's an user-provided, clickable link. You already have the infrastructure to turn user-provided links into clickable links (otherwise, you have a major security issue), so all you need to do is plug all those links into a decorator function.
The decoration part: have a function accept an URL argument. If the first part of the argument is "http://your.domain.name", or if it's a relative link (does not start with a protocol like http://), leave it alone. Otherwise:
$url = "http://your.domain.name/outgoing.php?".urlencode($url);
This will ensure that any unprotected characters in the original URL are properly escaped.
The redirection part: in the outgoing.php script simply look inside array_keys($_GET) to find the URL and display the appropriate page.
The best way would be to change your links where the new URL would be a script of your own that redirects users based on the query string. If you want to keep the same URL structure as deviantArt, use $_SERVER['QUERY_STRING'] to grab the URL in your redirect script.
Excellent help Netcoder. This is the full php of the The redirection part:
<?php
$redirect = $_SERVER['QUERY_STRING'].'';
?>
<html><head>
<meta name="robots" content="noindex" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Your Page title</title>
</head><body>
your Site Content
</body>
<SCRIPT LANGUAGE="JavaScript">
setTimeout("location.href ='<?php echo $redirect ?>'",30);
</script>
</html>
your Site Content is what you want to show. 30 is the time in
seconds to wait before the redirection. Save it as outgoing.php (or
anything)
External link must be in this format : http://your-nice-domain.com/outgoing.php?http://the-external-domain.com/external-path.any
Important :
Robot out this php file from robots.txt
It will be the same like dA except : the redirection is automatic.
There are a few ways you could approach this, depending on how the pages are generated. If all the links are being generated by PHP, then it could easily be done by comparing the URL of the link to that of the site. If they are different, prepend your passthough page to the beginning of the URL, similar to how DA did it.
I just completed a script to add external link icons next to each link, similar to how Wikipedia does it. For this particular site, I had to work with many pages that have static content. Using the PHP method I mentioned above, would not work well for this. Instead I implemented something in Javascript. Using jQuery, I select all anchor tags that start with "http://" or "https://", then I cycle through each one to check if the domain matches my current one. If it is different, I add the icon next to the link. You could do similar, but instead prepend the href of the anchor with your passthough page.
<?php
$html = 'Something'
. 'My site!';
echo preg_replace_callback('/<\s*a\s+[^>]*href\s*=\s*["\']?([^"\' >]+)["\' >]/', function($m) {
// initialise the data variable from your object
$link = $m[1];
// early return
if(strpos($link, 'http://example') === 0) return $m[0];
$linklen = strlen($m[1]);
$start = strlen($m[0]) - $linklen - 1;
$replacement = 'http://example.com/mymask?link=' . urlencode($link);
return substr_replace($m[0], $replacement, $start, $linklen);
}, $html);
I have a
my_text
link on my page, and the following line in my_redirect_page.php:
header("Location: ".$mylink);
but after the redirection, if I click on back in my browser, the "my_text" for the link does not appear as visited (in purple, instead of blue). How do I work around this? Is there a way to change the visited property in php or javascript?
Thanks,
Dave
Not a terrific solution, but, in my_redirect_page.php:
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=<?php echo $_GET['link']; ?>">
</head>
<body>
Redirecting to <?php echo html_entities( $_GET['link'] ); ?>.<br>
If you are not redirected, click here.
</body>
</html>
Or something like that - the Page should load (thereby entering into the browser history) and then, with a delay of 0, load the targeted URL. Should, for some reason, the redirect fail, the user will see a page containing a link to the targeted URL.
I'm not sure this is possible, unless you change the way your redirects are done.
[This question][1] is basically a duplicate of yours, and the consensus was that none of the browsers allow you to set pseudo-classes (like :visited).
The easiest way to simulate it for the user is to set a CSS class which colours the link to look the same as a browser default or CSS-style visited link, which you can easily do in your view layer or by adding the class using javascript if the link appears in window.history.
You may also be able to push elements onto the window.history array, and have them appear in the browser history (and hence be given the :visited pseudoclass), but I'm not sure if that would work. Worth a try though.