Page 1 of 3

Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 12:39 pm
by JKM
Hi there, I've got this script for a form, and I wan't to redirect logged in users to f.ex google.com. But nothing happends. Don't mind the simple password solution.

Code: Select all

if($_POST['submit'] == TRUE) {
    if($_POST['password'] == '123') {
        header('Location:http://google.com');
    }
    else {
        echo 'Wrong password!';
    }
}

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 12:40 pm
by ghurtado
Can you post the page that submits to this one?

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 12:46 pm
by JKM

Code: Select all

<form enctype="multipart/form-data" action="" method="post">
    <input type="password" name="password" />
    <input type="submit" name="submit" value="GO!" />
</form>
The "Wrong password!" sentence appears if the password is wrong, it's only the redirect that won't work.

Edit: Warning: Cannot modify header information - headers already sent by (output started at /home/xx/index.php:6)

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 12:58 pm
by jayshields
Did you read the header() page in the manual? You should be enclosing the parameter value with double quotes and a trailing slash. Also, have an exit call after the header() call. Also make sure there is no output before the header() call, if you have error_reporting() set to something sensible you should already have rectified this problem if it is the cause.

Another thing to change in your code is $_POST['submit'] == TRUE to something like isset($_POST['submit']).

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 2:51 pm
by Ziq
Maybe problem with attribute action in form? If with code collect in file index.php it worked fine.
Try: (index.php)

Code: Select all

 
<?php
if($_POST['submit'] == TRUE) {
    if($_POST['password'] == '123') {
        header('Location:http://google.com');
    }
    else {
        echo 'Wrong password!';
    }
}
?>
<form enctype="multipart/form-data" action="" method="post">
    <input type="password" name="password" />
    <input type="submit" name="submit" value="GO!" />
</form>
 

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 2:55 pm
by Ziq
You should be enclosing the parameter value with double quotes and a trailing slash
it isn't necessary. header()

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 2:56 pm
by JKM
This didn't work either!

Code: Select all

<form enctype="multipart/form-data" action="" method="post">
    <input type="password" name="password" />
    <input type="submit" name="submit" value="GO!" />
</form>
<?
    if($_POST['submit'] == TRUE) {
            if($_POST['password'] == '123') {
            header("Location: http://google.com/");
        }
        else {
            echo 'Wrong password!';
        }
    }
    error_reporting(E_ALL);
?>

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 3:18 pm
by Ziq
Read manual header()! Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

PHP code must be always on top (as a rule)! If you want to change error_reporting mode input it in top code.

It's work fine:

Code: Select all

 
<?
    error_reporting(E_ALL);    
    if($_POST['submit'] == TRUE) {
            if($_POST['password'] == '123') {
            header("Location: http://google.com/");
        }
        else {
            echo 'Wrong password!';
        }
    }    
?>
<form enctype="multipart/form-data" action="" method="post">
    <input type="password" name="password" />
    <input type="submit" name="submit" value="GO!" />
</form>
 
I just sorted piece of code.

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:01 pm
by JKM
Warning: Cannot modify header information - headers already sent by (output started at /home/xx/index.php:8) in /home/xx/index.php on line 10

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LOLL</title>
</head>
<body>
<?
if($_POST['submit'] == TRUE) {
    header("Location: http://google.com/");
    exit;
}
error_reporting(E_ALL);
?>
<form enctype="multipart/form-data" action="" method="post">
    <input type="submit" name="submit" value="GO!" />
</form>
</body>
</html>
Doesn't work...

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:08 pm
by stevoo

Code: Select all

 
<?
 if($_POST['submit'] == TRUE) {
     header("Location: http://google.com/");
     exit;
 }
 error_reporting(E_ALL);
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>LOLL</title>
 </head>
 <body>
 
 <form enctype="multipart/form-data" action="" method="post">
     <input type="submit" name="submit" value="GO!" />
 </form>
 </body>
headers should be called before anything else as it was stated above

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:13 pm
by Ziq
JKM wrote:Warning: Cannot modify header information - headers already sent by (output started at /home/xx/index.php:8) in /home/xx/index.php on line 10

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LOLL</title>
</head>
<body>
<?
if($_POST['submit'] == TRUE) {
    header("Location: http://google.com/");
    exit;
}
error_reporting(E_ALL);
?>
<form enctype="multipart/form-data" action="" method="post">
    <input type="submit" name="submit" value="GO!" />
</form>
</body>
</html>
Doesn't work...
It isn't my code!

Closely look at my post! I repeat: Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

READ MANUAL!!!

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:19 pm
by stevoo

Code: Select all

<?
     if($_POST['submit'] == TRUE) 
             header("Location: http://google.com/");
         else 
             echo 'not Yet!';
         
       
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>LOLL</title>
  </head>
  <body>
  
  <form enctype="multipart/form-data" action="" method="post">
      <input type="submit" name="submit" value="GO!" />
  </form>
  </body>
This will work

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:22 pm
by Ziq
If he does not think of the decision, he never becomes the programmer

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:23 pm
by JKM

Code: Select all

<?
if($_POST['submit'] == TRUE) {
    header("Location: http://google.com/");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LOLL</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
    <input type="submit" name="submit" value="GO!" />
</form>
</body>
</html>
Doesn't work.

Code: Select all

<?
    header("Location: http://google.com/");
    exit;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LOLL</title>
</head>
<body>
&nbsp;
</body>
</html>
Doesn't work.

Code: Select all

<?php
header("location: 1.html"); 
?>
Doesn't work.

Re: Need help with redirecting in PHP

Posted: Tue Aug 26, 2008 4:31 pm
by Ziq

Code: Select all

 
<?php
header("location: 1.html");
?>
 
Doesn't work.

Ok. What mean "Doesn't work"? Any errors? Try

Code: Select all

 
<?php
error_reporting(E_ALL);
header("Location: 1.html");
?>