Redirecting in php

Show off new creations, get help, or just chat about all aspects of web development and design.

Redirecting in php

Postby davidc538 on Fri Jul 17, 2009 8:14 pm

Code: Select all
header("Location: /surveys.php");

This line of code simply doesn't work, its driving me nuts. XAMPP is simply echoing
Code: Select all
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SSA\login.php:5) in C:\xampp\htdocs\SSA\login.php on line 17

AND I DON'T KNOW WHAT THE HELL IT MEANS!!!!:@:@:@

seriously though, if anyone can help me sort this out it'd be much appreciated
all i need to do is execute some code in php to send the user to another page of my choice

thanks in advance
User avatar
davidc538
Been Here A While
Been Here A While
 
Joined: Mon Aug 11, 2008 4:27 pm

Re: Redirecting in php

Postby Blink on Fri Jul 17, 2009 9:05 pm

Are you declaring it before any other code in your page?

In my limited PHP experience if you try to declare PHP headers after the page has already requested them then you get issues.
User avatar
Blink
Cool 'n that
Cool 'n that
 
Joined: Fri Oct 08, 2004 4:16 pm
Location: UK

Re: Redirecting in php

Postby zombie@computer on Fri Jul 17, 2009 9:10 pm

headers are supposed to be the first thing you send to a browser, before the page's contents even get sent. Make sure you are not outputting any other data before the header() line(s)
When you are up to your neck in shit, keep your head up high
User avatar
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Redirecting in php

Postby abathor on Tue Jul 21, 2009 6:25 pm

I thought you were supposed to use headers in PHP to redirect, like you are, I'm guessing, trying to do. It worked for me before, I'm pretty sure you can redirect from anywhere in the php, and that it doesn't have to be from the top of the page.
User avatar
abathor
1337 p0st3r
1337 p0st3r
 
Joined: Mon Dec 18, 2006 6:02 pm
Location: England

Re: Redirecting in php

Postby zombie@computer on Tue Jul 21, 2009 6:30 pm

abathor wrote:I thought you were supposed to use headers in PHP to redirect, like you are, I'm guessing, trying to do. It worked for me before, I'm pretty sure you can redirect from anywhere in the php, and that it doesn't have to be from the top of the page.

you are wrong

php.net/manual/en/function.header.php
When you are up to your neck in shit, keep your head up high
User avatar
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Redirecting in php

Postby Spirit55555 on Tue Jul 21, 2009 8:29 pm

zombie@computer wrote:
abathor wrote:I thought you were supposed to use headers in PHP to redirect, like you are, I'm guessing, trying to do. It worked for me before, I'm pretty sure you can redirect from anywhere in the php, and that it doesn't have to be from the top of the page.

you are wrong

php.net/manual/en/function.header.php

Not entirely...

You can use Output Buffering Control to send headers anywhere in PHP:
http://www.php.net/manual/en/book.outcontrol.php
"Work smart, not hard"
User avatar
Spirit55555
Dumpling
Dumpling
 
Joined: Sun Aug 03, 2008 9:58 am
Location: Denmark

Re: Redirecting in php

Postby zombie@computer on Tue Jul 21, 2009 9:52 pm

Spirit55555 wrote:
zombie@computer wrote:
abathor wrote:I thought you were supposed to use headers in PHP to redirect, like you are, I'm guessing, trying to do. It worked for me before, I'm pretty sure you can redirect from anywhere in the php, and that it doesn't have to be from the top of the page.

you are wrong

php.net/manual/en/function.header.php

Not entirely...

You can use Output Buffering Control to send headers anywhere in PHP:
http://www.php.net/manual/en/book.outcontrol.php

Yes, well, you can calculate pi in php in seven hundred iterations and then header() redirect, as long as you dont send any output. Same for OB. I would hardly call that 'anywhere in the php'.
When you are up to your neck in shit, keep your head up high
User avatar
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Redirecting in php

Postby BruceSmith on Mon Nov 30, 2009 8:13 am

Hi,
I got this problem and it took nearly 5 days to solve.
The function you wrote already seems error. There shouldn't be forward slash before file.
There should not be any space after the function. Try to remove semicolon after function.
I don't exactly what else I did. But I will soon clarify you.
BruceSmith
Dumpling
Dumpling
 
Joined: Sat Nov 28, 2009 8:05 am

Re: Redirecting in php

Postby Terr on Mon Nov 30, 2009 10:53 am

As others have said, it's complaining because some of your code--or some code in whatever PHP framework you're running--has already sent off the batch of headers that web browsers expect first before anything else. There are other ways to do redirects (javascript and meta-refresh tags) but headers should generally be your first tool of choice.

Another thing to ensure is that there is no space--even blank space-- before your first <?php opening tag in the document. Because that suggests you want to sent one character of web-site content before even getting to your code.
Terr
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Mon Oct 12, 2009 11:35 pm

Return to Web Design & Development

Who is online

Users browsing this forum: No registered users

cron