Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: Php: Redirecting In Php

(1,198 views • 17 replies)

This topic is 1 page long.

<< < > >>
None

authorblues

Reply To Post Reply & Quote

Posted at: 8/1/06 05:45 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,263

you might want to note that the header function does NOT halt script execution. if you want to stop anything from happening (such as formal redirection), you need to call the exit or die functions right after header('Location:');.

in other words, if you call the header function to redirect, and then afterwards you have a piece of script that modifies a file or database, the header redirect will NOT stop the modification from taking place.

i always include a special function in my layout class to handle redirections. something like:

function formalRedirect($u){
header("Location: $u");
exit;
}

my blog. smash code: 3523-1700-9216 [AUTHR]

BBS Signature

None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/1/06 06:07 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,147

Don't forget, you can't use the header function after any HTML. I had a bunch of problems with that when I was starting with it.


None

DFox

Reply To Post Reply & Quote

Posted at: 8/1/06 06:16 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,216

It seems like a good tutorial but PHP?>...? I've never heard of ending a PHP script like that.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/1/06 06:19 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,147

At 8/1/06 06:15 PM, Plakat wrote: Of course not. It should be the first thing. IMO.

Usually, when I redirect, that is the only thing the page is meant for, just so nothing gets weird. That's what I would suggest for anyone starting with header().

At 8/1/06 06:16 PM, GamesCool wrote: It seems like a good tutorial but PHP?>...? I've never heard of ending a PHP script like that.

Actually, me neither.


None

authorblues

Reply To Post Reply & Quote

Posted at: 8/1/06 06:42 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,263

At 8/1/06 06:19 PM, WoogieNoogie wrote:
At 8/1/06 06:15 PM, Plakat wrote: Of course not. It should be the first thing. IMO.
Usually, when I redirect, that is the only thing the page is meant for, just so nothing gets weird. That's what I would suggest for anyone starting with header().

why not just work with output buffering. then it doesnt matter when you send headers...

my blog. smash code: 3523-1700-9216 [AUTHR]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 8/1/06 06:43 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,216

At 8/1/06 06:42 PM, authorblues wrote: why not just work with output buffering. then it doesnt matter when you send headers...

You could, but I find it cleanest to do all my PHP at the top of the page, so a redirect would never come into play after content was outputted. Then again that's just how I code.


None

authorblues

Reply To Post Reply & Quote

Posted at: 8/1/06 06:47 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,263

At 8/1/06 06:43 PM, GamesCool wrote: You could, but I find it cleanest to do all my PHP at the top of the page, so a redirect would never come into play after content was outputted. Then again that's just how I code.

i used to do the same thing, but its often awkward to workout where you put your headers. i find it easier to not worry about it by introducing output buffering. thats not to say that i dont stick all my php at the top, and attempt to get my headers sent first, but it just keeps me from doing some awkward workarounds.

my blog. smash code: 3523-1700-9216 [AUTHR]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 8/1/06 06:50 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,216

At 8/1/06 06:47 PM, authorblues wrote: i used to do the same thing, but its often awkward to workout where you put your headers. i find it easier to not worry about it by introducing output buffering. thats not to say that i dont stick all my php at the top, and attempt to get my headers sent first, but it just keeps me from doing some awkward workarounds.

Yeah, I know what you mean, and that's definitley a good point.

Output buffering can definitley be useful at times.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/9/06 02:00 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

if you want to use the header() to go to a page that is in teh same folder as the php script, can u just go

header('page.php')


None

elbekko

Reply To Post Reply & Quote

Posted at: 8/9/06 02:03 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,302

At 8/9/06 02:00 PM, thecoshman wrote: if you want to use the header() to go to a page that is in teh same folder as the php script, can u just go

header('page.php')

Yeah :)

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

Craige

Reply To Post Reply & Quote

Posted at: 8/9/06 03:34 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,070

At 8/1/06 05:45 PM, authorblues wrote: you might want to note that the header function does NOT halt script execution. if you want to stop anything from happening (such as formal redirection), you need to call the exit or die functions right after header('Location:');.

hmm, I never thought about that before. Makes sence though.


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 8/9/06 05:37 PM

Pilot-Doofy LIGHT LEVEL 37

Sign-Up: 09/13/03

Posts: 12,297

At 8/9/06 03:34 PM, Craige wrote:
At 8/1/06 05:45 PM, authorblues wrote: you might want to note that the header function does NOT halt script execution. if you want to stop anything from happening (such as formal redirection), you need to call the exit or die functions right after header('Location:');.
hmm, I never thought about that before. Makes sence though.

I believe that header() halts execution as long as you're not using an output buffer but I'm not sure.

holy jesus what are these goddamn animals


None

henke37

Reply To Post Reply & Quote

Posted at: 8/10/06 06:12 AM

henke37 NEUTRAL LEVEL 16

Sign-Up: 09/10/04

Posts: 2,462

Totaly wrong. Header is like any other function and normal functions doesn't terminate the script.

I used to recommend my article about php security, but my server is down, so all I say is: ActionScript version 2 sucks big time! Go learn ActionScript version 3 already!


None

Craige

Reply To Post Reply & Quote

Posted at: 8/10/06 10:48 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,070

Yeah, the PHP.net site confirms that the function doesn't halt script exicution.


None

DFox

Reply To Post Reply & Quote

Posted at: 8/10/06 10:51 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,216

At 8/10/06 10:48 PM, Craige wrote: Yeah, the PHP.net site confirms that the function doesn't halt script exicution.

Yeah. It even shows exit; used after a header("location: tgr.com") call to stop the script.


None

Craige

Reply To Post Reply & Quote

Posted at: 8/11/06 12:53 AM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,070

At 8/10/06 10:51 PM, DFox2 wrote: Yeah. It even shows exit; used after a header("location: tgr.com") call to stop the script.

Yeah, I guess you learn something new every day.


None

Rantzien

Reply To Post Reply & Quote

Posted at: 9/4/06 11:31 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,421

Also, if you are working with sessions, the header function will not pass the session ID. This means session data will be lost unless you pass on the session ID manually, which must be done with the SID constant.

For example:
header ("Location: mypage.php?" . strip_tags (SID));

BBS Signature

None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 7/21/07 11:49 AM

Pilot-Doofy LIGHT LEVEL 37

Sign-Up: 09/13/03

Posts: 12,297

Why would you say you have to include a protocol (http, etc.) in the header call? That is simply untrue. I call header with relative linking all the time. In addition, like others said, you should encourage halting the script when a redirect is encountered.

Say, for instance, the script is on a page that would delete a private message from the database. Well, if they aren't logged in or it isn't their message to delete in the first place, steer them away from the page by using a redirect. However, if you don't halt the execution of the page, the message would still end up being deleted.

Even if they don't know if they succeeded, the damage is still done. I also don't think it would have been a bad idea to touch lightly on output buffers in this tutorial. If nothing else, give them a brief description and explanation and link to another tutorial (I'm sure there is one).

All in all, it wasn't too bad. But I think the main thing is to get totally know-it-all about the topic you're writing a tutorial on, otherwise the users aren't informed as well as they could be.

holy jesus what are these goddamn animals


All times are Eastern Daylight Time (GMT -4) | Current Time: 11:34 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!