Alternamaton Posted June 11, 2007 Share Posted June 11, 2007 I can't figure out how to use a submit button to open a new web page. I don't want to use "action=somepage.php" because I'm doing the form authentication on the form's page. Basically, a user puts in their username and password, and I run some conditional statements which check my database and make sure that the user is valid, and if they are valid I want them to be redirected to homepage.php. But again, I don't want the authentication to be done on homepage.php. Shouldn't there be some simple built-in function to go to a new page? I looked through my reference manual and did several searches on here, and all I found was fopen and header, neither of which are what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/ Share on other sites More sharing options...
AndyB Posted June 11, 2007 Share Posted June 11, 2007 header("Location: homepage.php"); Arrange your logic so that (on success) there is no output to the browser before the header line Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272113 Share on other sites More sharing options...
Alternamaton Posted June 11, 2007 Author Share Posted June 11, 2007 That makes no sense. How can there be no output to the browser before the header if I have an HTML form that the user has to fill out before they're redirected? Is there really no built-in PHP function that acts just like an HTML link? I.E., if (isset($_POST['login'])) { # Some conditionals to authenticat the form... open_page("http://www.google.com"); } And boom, they fill out the form, it's authenticated, and if everything checks out, they go to www.google.com. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272118 Share on other sites More sharing options...
AndyB Posted June 11, 2007 Share Posted June 11, 2007 It might make no sense to you, but it's exactly how it's done. It's all in the logic/flow of your code. if the submit button has been clicked if all validation checks succeed header(location .... else do your html stuff See that - no output to the browser before the header function Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272123 Share on other sites More sharing options...
Alternamaton Posted June 11, 2007 Author Share Posted June 11, 2007 I thought that there had to be no output to the browser before the header function, period (i.e., before even <html>). Okay, here's a simplified example of the code I'm working with: <html> <body> <form method="post"> <input type="submit" name="redirect" value="Home page"> </form> <?php if (isset($_POST['redirect'])) { header("Location: http://localhost/homepage.php"); } ?> http://localhost/homepage.php exists. When I click the submit button, I receive the following error message: Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\redirecttest.php:6) in C:\wamp\www\redirecttest.php on line 9 Line 9 is: header("Location: http://localhost/homepage.php"); And homepage.php (which is nothing more than <html><body>Welcome.</body></html>) doesn't load. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272133 Share on other sites More sharing options...
AndyB Posted June 11, 2007 Share Posted June 11, 2007 I thought that there had to be no output to the browser before the header function, period (i.e., before even <html>). You're right. Your simplified example fails that test, my suggested logic/flow passes it. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272136 Share on other sites More sharing options...
TEENFRONT Posted June 11, 2007 Share Posted June 11, 2007 fella... you aint listening! <?php if (isset($_POST['redirect'])) { header("Location: http://localhost/homepage.php"); } ?> <html> <body> <form method="post"> <input type="submit" name="redirect" value="Home page"> </form> Do the php shizzle BEFORE any browser output. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272138 Share on other sites More sharing options...
Alternamaton Posted June 11, 2007 Author Share Posted June 11, 2007 So you can have the conditional statements before the header statement; you just have to do it before <html>. I thought that the header statement had to be the very first thing, period (which would make it hard to do anything with PHP before the header!). Simpy putting all the php before <html> works beautifully. Sorry if I came off sounding impatient. I didn't understand that "before any output to the browser" simply means "before <html>". Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272140 Share on other sites More sharing options...
TEENFRONT Posted June 11, 2007 Share Posted June 11, 2007 well it doesnt plainly mean "before the <html>" .. EG <?php if($var) { echo "weeeee"; header("location: http://"); } ?> <html> That would error on headers already sent. as weeee is an ouput. Do things in php with if and elses and vars and you wont need to output anything at all untill you actually want to show the visitor something. Quote Link to comment https://forums.phpfreaks.com/topic/55045-solved-silly-question/#findComment-272143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.