Page 1 of 1

redirecting a user dependent on a variable

Posted: Tue Nov 21, 2006 5:21 am
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

Posted: Tue Nov 21, 2006 6:00 am
by JayBird
header('Location: somepage.php'); will work fine as long as you haven't output anything to the browser

Posted: Tue Nov 21, 2006 6:15 am
by hame22
the problem here is that I have, is there any way around this?

thanks

Posted: Tue Nov 21, 2006 6:24 am
by JayBird
yes, you can use output buffering.

Otherwise, dont display anything before redirecting the user.

Posted: Tue Nov 21, 2006 7:24 am
by hame22
how is this done, is it quite easy? Or is it complex for just adding a login script?

thanks for your help

Posted: Tue Nov 21, 2006 7:31 am
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.

Posted: Tue Nov 21, 2006 8:02 am
by JayBird
Yes, as feyd said, it would be better to rethink your logic to achieve what you want

Posted: Tue Nov 21, 2006 8:25 am
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

Posted: Tue Nov 21, 2006 8:49 am
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.

Posted: Tue Nov 21, 2006 8:53 am
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