Creating visible redirect? - php

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

Related

how to set more than one cookie through PHP at a time?

Well guys, this is my very first time over here, so, hi community.
I'm working in a small school project which requires that I set 2 different cookies (one for saving an Username and other for changing the background color), and as the question title says, it is restricted to PHP only, so I'm constantly receiving: \"Cannot modify header information – headers already sent". So, i'm trying to make this work.
<?php
setcookie('name', $_REQUEST['name'], time()+60*60);
setcookie('color', $_REQUEST['color'], time()+60*60);
header("location:name_of_the_web");
?>
So, in my Index page, I asked for both of the request info, color and name.
<?php
echo "<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Temario</title>
</head>
<body>";
if(!isset($_COOKIE['name']) && !isset($_COOKIE['color'])){
echo "<form method='post' action='cookies.php'>
...//it keeps going with the form to fill it.
?>
I wonder if there is any other way to ask for both of the cookies at the time? OR if that is wrong? OR what I may be doing wrong so it sends me the header modification error?
Thx.
Added:
I just found that the mistake shows up when I'm using both of the
\setcookies(args);\ when I'm using only one of them it runs ok, so, may I request the cookies changes in different moments? For instance saving the username in the Index and afterwards changing the background color?
So you are recieving "Cannot modify header information – headers already sent".
This is an error causes in php page if output from .php file is already printed on the page before header modification.
Example
If you try to do something like
<?php
echo "hello";
header("location: anotherPage.php");
?>
Or
<h1> hello <h1/>
<?php
header("location: anotherPage.php");
?>
Then you will get that error in both the cases. Because echo statement has printed something on the screen before the header() call.
So you just have to keep in mind nothing should get printed on the page before header() call.
Else your code is perfect
<?php
setcookie('name', $_REQUEST['name'], time()+60*60);
setcookie('color', $_REQUEST['color'], time()+60*60);
header("location:name_of_the_web");
?>
Just keep in mind not to print anything on the page neither from html nor from php on same .php file.
You can use forms to go run another php page from current page and then set cookies and header back to the same or another page. This will not cause that error. ☺

Return Redirect with body in Laravel

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/" />

Php Location("some other page")

I'm new to PHP and this is something that I don't know how to do, even though I have been searching it.
I know that redirecting can be made with Location("some page"). I also read that this works just if there is nothing displayed to user.
What I want to do is:
Display a message to user. echo "message.redirecting...."
Wait for 2 seconds sleep(2);
Then redirect Location("some page");
Any ideas?
Andrew
This is part of an assignment and javascript is not allowed. Only PHP.
You can use a meta refresh, which is just a html meta tag placed inside the <head> of your page. Like this:
<meta http-equiv="refresh" content="2;url=http://newurl.com/">
This will redirect the page to the http://newurl.com after 2 seconds.
Do not do it this way.
It's VERY bad usability.
And there is not much sense in saying "redirecting".
That's legacy of ancient ages of raw HTML sites. No good site using such redirects these days.
Redirect with no messages.
Unfortunately you can't do that. header() calls, such as header('Location: '); rely on http headers, which have to be sent before any output is sent to the client.
I reccomend using a Javascript Redirect if you want a message displayed to the users.
<html>
<head>
<script type="text/javascript">
<!--
function delayer(){
window.location = "../javascriptredirect.php"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">
<h2>Prepare to be redirected!</h2>
<p>This page is a time delay redirect, please update your bookmarks to our new
location!</p>
</body>
</html>
or you can do a php redirect like this:
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
?>

How can I redirect using a PHP script called from an SSI?

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"

PHP Change Page?

I have found out the bad news about header(), so I am no longer using it, because now my website doesn't work..
Is there any other simple way to change pages automatically?
you mean like redirect?
you can also use this:
<meta http-equiv="Refresh" content="5;
URL=http://www.yahoo.com">
Try javascript redirection
<script type='text/javascript'>
window.location = "new_page.php";
</script>
or you can use meta-refresh (many examples in google).
If you can't use header in your script, just add ob_start(); at the beginning and then you can use it AFTER html.
Ultimately you'd do well to re-work your code so you do a redirection before sending a bunch of HTML that's never going to be seen. And if you do that - you can use header(), which causes less delay for your end users, reduces processing all round, and is search-engine friendly! Win-win-win.

Categories