Jump to content

[SOLVED] Silly question


Alternamaton

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.