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.
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'm novice in php and already searched the forum for this and found something similar although i don't understand it
i have 2 external links http://www.hello.com and http://www.bye.com
on click on each link, i want to load an intermediate page (eg: redirect.php) for 5 second with a
message: "You are leaving misite.com. goodbye."
and then, make the correct to the correct external link that applies in each case.
i need:
1.- content of redirect.php
Continue to external link
Continue to external link
i'm sure it's wrong.
also, i don't know how to put a 5 seconds spinner (gif) an then make the redirection (step3)
2.- link users click on it. is it ok?
i have:
hello
not sure if it's correct
the link the users hit, tries to call the link in the redirect page, but i'm unable
well, the thing i don't know how to make it work and i really aprecciate an example
thanks
You should make your link to redirect.php, and add the "final destination" as a URL parameter, as you have already done. On redirect.php, use a meta tag in the head section that redirects (see here for an example).
EDIT: Code requested...
In your main php page, have your links as you already wrote:
Hello
Goodbye
In redirect.php, you need to add a meta tag in the <head>...</head> section of your HTML:
<html>
<head>
...
<meta http-equiv="refresh" content="5;url=<?php echo $_GET['link'];?>" />
...
</head>
<body>
<h1>You are leaving my site!</h1>
<img src="/images/spinner.gif" alt="spinner" />
</body>
</html>
and that's it. The meta tag "content" attribute contains the number of seconds before redirecting, and the address to redirect to.
Hope this helps.
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>
On a WAMP server, I have a server-side include in a file, a.shtml, composed of the following code:
<!--#include virtual="./req.php"-->
The contents of req.php are:
<?php
Header("Location:index.php");
echo "still here";
?>
When I open a.shtml, I see the text still here, but the page has made no attempt to redirect itself. Why is this? And is there any way to make it work?
Thanks for the help
EDIT: The reason I want to do this is because I have some session variables that I want to influence the way the PHP script acts. If the session variables are not set, I need it to redirect to a login page. I know I can just write the entire thing in PHP, but I'd like to do it this way if possible. If it's not possible to change header information from an included PHP file from SSI, then I'll just do it entirely in PHP.
it's impossible
you don't need that.
just address tour script that set session variables directly, not through ssi
MAYBE (with capital letters Lol), you can pull this off if you call that script in an IFRAME and that IFRAME outputs some JScript like window.parent.location = <some_url_here> forcing its parent to change its location... Its just fast-thinking from my part, I might be wrong with IFRAMEs' parent-child relation to the original document, as I haven't tested the "idea" myself :)
If your req.php returns the following html code, the redirect will happen:
<html><head>
<title>HTTP 301 This page has been moved</title>
<meta http-equiv="Refresh" content="0;URL=https://www.example.com/index.php">
</head>
<body></body></html>
But: "Google Warning: Using The Meta Refresh Tag Is Bad Practice"
Please Pardon if the question sounds silly, but nevertheless its a question which I want to know. :)
How can I redirects which display that you are being redirected (like older Gmail and LinkedIn). Whenever I tried to do that, I got errors saying that Headers were already sent.
Then somebody here told me that I should not output any markup before redirection (like facebook login). So how do I go about it and display something nice during redirection??
you want to use meta redirects. they enable you to show a page, and after a few seconds this page will send you to the new page. all you have to do is add a tag to the portion of your 'something nice' redirection page.
here's a quick tutorial on meta redirects:
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm
you want to do something like this:
<html>
<head>
<meta http-equiv="refresh" content="2;url=whereto">
were 2 is the number of seconds to display your page and whereto is where you want to send your user
You need to output your page, which will include the following META keyword:
<html>
<head>
<!-- redirection to example.com in 5 seconds -->
<meta http-equiv="refresh" content="5;url=http://example.com/" />
...
</head>
<body>
...
Read the following article for more help: http://en.wikipedia.org/wiki/Meta_refresh
Those redirections are not done via "normal" redirection HTTP headers. Instead they display a page and use either a META Refresh or some Javascript to navigate to the new page.
Personally I find both methods not very nice, both for users and for search engines. Using HTTP headers that also signify why there is a redirect (Permanently moved, temporary, &c.) are way better imho.
If you are getting the headers already sent then that means you were trying to redirect with a PHP header() redirect and you had output on the screen before calling the header() function, 1 solution to that is to use PHP's Output Buffering which will then allow you to call the header() redirect anywhere on the page and not get that error.
To show a message you could use the meta method mentioned in some other answers maybe even throw in a javascript redirect with it, just to be safe however the way I would do it would be something like this below
<?PHP
ob_start();
//show a message
echo 'You will be redirected in 10 seconds';
//amount of time to show the message above before redirecting
sleep(200);
//redirect
header('http://domain.com');
?>
I did not test this but you should get the idea, actually now that I think about it, sleep() might not work correctly as expected with output buffering