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

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

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

Nuwan
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.

Reply

dserban
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.

Reply

vujsa
What you need here is the header function.

Like so:
CODE
<? php
if($_POST['username'] == 'Nuwan'){
     header('Location: http://www.example.com/authorized.php');
}
else{
     header('Location: http://www.example.com/login.php');
}
?>

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. cool.gif

vujsa

Reply

Nuwan
Hey Guys..

Thanks for all the help. I got it working now, by using header ('Location : url');. Ya i got the database stuff figured out.
Thanks,
Nuwan.

Reply

pyost
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.

CODE
<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 wink.gif

Reply

galexcd
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?

Reply

pyost
Because it would have to be located between the HEAD tags wink.gif 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.

Reply

jlhaslip
QUOTE(pyost @ Sep 14 2007, 02:32 AM) *
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.

CODE
&lt;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 wink.gif

Or learn how to use ob_start(), and the associated functions.
QUOTE
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.

 

 

 


Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. forward url javascript - 34.11 hr back. (1)
  2. php forward url - 36.03 hr back. (1)
Similar Topics

Keywords : forward, url, php, forward, url

  1. How To Make Hotmail Auto Forward Mails?
    (12)


      Looking for forward, url, php, forward, url






*SIMILAR VIDEOS*
Searching Video's for forward, url, php, forward, url
advertisement




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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE