Jump to content
xisto Community
Sign in to follow this  
Nuwan

Forward To Another Url In Php How to forward to another URL

Recommended Posts

Hey Guys.. I'm using PHP to develop a small login module. I need to forward the user to a new page if the user has entered a valid user name and password. I submit the form to the same page which has the login form, and does the validations there. I do not know how to forward the user to another page by checking a certain condition in php. Please be kind enough to help me out here. Thanks in advance, Nuwan.

Share this post


Link to post
Share on other sites

I think you just output a JavaScript tag with instructions for the redirection if the condition is met.If the condition isn't met, the script tag simply won't exist, and therefore the redirect won't happen.BTW, your topic belongs in the PHP forum.

Edited by dserban (see edit history)

Share this post


Link to post
Share on other sites

What you need here is the header function.

Like so:

<? phpif($_POST['username'] == 'Nuwan'){	 header('Location: http://forums.xisto.com/no_longer_exists/{	 header('Location: http://forums.xisto.com/no_longer_exists/;
Of course, you'll need a way to compare your username and password to a database or list of users but you didn't ask that question so I'll assume for now that you either don't need it yet or have already figured that out.

Hope this helps. :blink:

vujsa

Share this post


Link to post
Share on other sites

It is only a matter of time until you receive an error message saying "Headers already sent" - this will, of course, disallow you to redirect the user to another page. This is because you have already outputted some data to the page. If you ever encounter this problem, and you can't avoid it, you will have to use JavaScript.

<script type="text/javascript">window.location = "Redirection URL";</script>

As this can be used anywhere in the page, it is more convenient, but header() is always a better option :blink:

Share this post


Link to post
Share on other sites

Using javascript the redirect a page is the worst way ever! If you have to do something in the actual code php spits out why not just use meta tag to redirect?

Share this post


Link to post
Share on other sites

Because it would have to be located between the HEAD tags :blink: As I already said, it's JavaScript you have to use if the redirect is to occur after displaying some text on the page. If you didn't start the BODY tag yet (and therefore can use the META redirect), you can probably rewrite your code to use header() instead.

Share this post


Link to post
Share on other sites

It is only a matter of time until you receive an error message saying "Headers already sent" - this will, of course, disallow you to redirect the user to another page. This is because you have already outputted some data to the page. If you ever encounter this problem, and you can't avoid it, you will have to use JavaScript.

 

<script type="text/javascript">window.location = "Redirection URL";</script>
As this can be used anywhere in the page, it is more convenient, but header() is always a better option :blink:
Or learn how to use ob_start(), and the associated functions.

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.