redirecting a user dependent on a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

redirecting a user dependent on a variable

Post by hame22 »

Hi I have a login box on my website.

What I want to do is when the user logs in for the first time they are redirected to their personal details page else they remain on the page they have logged in.

I have tried user the header('Location') but where the login box are on the pages it is saying the headers have already been sent.

Is there another way to achieve this?

Thanks in advance
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

header('Location: somepage.php'); will work fine as long as you haven't output anything to the browser
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

the problem here is that I have, is there any way around this?

thanks
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yes, you can use output buffering.

Otherwise, dont display anything before redirecting the user.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

how is this done, is it quite easy? Or is it complex for just adding a login script?

thanks for your help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The general idea is to always do all logic processing before any output is sent so you can perform redirections and other dealings without being in the midst of output. This also lends itself to the concept many of us preach: separation of application logic from display logic.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yes, as feyd said, it would be better to rethink your logic to achieve what you want
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thats great thanks, I have seperated the login script from the login display.

What I would like to know is there a way of adding my login function $login = login($username, $password); to each page automatically, rather than going through each page and adding it manually?

Thanks again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's what FrontControllers, ApplicationControllers and PageControllers can do. Otherwise, you'll need to manually add it in some fashion. It may be as simple as including the right file and running a function at the proper time, it's hard to say without knowing your code more.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

the function just needs to run at the start of every page returning a variable $login which displays the appropriate login display. Could this be achieved by including a file that runs this function on the page?

thanks for your time
Post Reply