eveo
eveo

Reputation: 2833

PHP redirect to another page and pass value?

I have a problem where I need to redirect to another page and pass a value, which is easy and I know how to do, except that theres the old header problem: Cannot modify header information - headers already sent by ... line 17

<?php include('m_a2_functions.php'); ?>
<?php global_header(LOGIN); ?>
<?php login(); ?>
<?php 
if(isset($_POST['submit']))
{
    connect();
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT password from users where username = '$username'";
    $result = mysql_query($query) or die ("There was an error executing the query $query:<br/>".mysql_error());
    $row = mysql_fetch_array($result);
    $encrypted_pass = $row[0];

    if (crypt($password, $encrypted_pass) == $encrypted_pass) {
        $flag = "y";
        header('Location: m_a2_view.php?flag=y');
    }

}
?>
<?php validation(); ?>
<?php page_footer(); ?>

This is all fine, except that there is a problem with the header part. It won't redirect.

GLOBAL HEADER FUNCTION IN FUNCTIONS.PHP

<?php function global_header($page_title){ ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <- LINE 17
<!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-US" lang="en-US">
    <head>
        <title><?php echo "MARK - " . $page_title; ?></title>
        <link rel="stylesheet" href="style.css" />
    </head> 
    <body>
    <div id="container">
    <div id="logo"></div>
<?php } ?>

LINE 17

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

I know that's what is causing it. It loads global_header at the start of my page and then can't redirect to another page. Soon as I hit submit, my page should redirect, but it cannot!

Even if I start removing things, it won't work. It will EVENTUALLY work after getting rid of global_header() completely, something which I can't do.

Upvotes: 1

Views: 3608

Answers (2)

Baba
Baba

Reputation: 95101

The reason you are having this issue is because your script is multiple open <?php and closing ?> of PHP tag which has space and new line between them ...

Replace

<?php include('m_a2_functions.php'); ?>
<?php global_header(LOGIN); ?>
<?php login(); ?>
<?php 

With

<?php 
include('m_a2_functions.php');
//global_header(LOGIN);
//login(); 

Your Script

    include ('m_a2_functions.php');
    if (isset ( $_POST ['submit'] )) {
        connect ();
        $username = $_POST ['username'];
        $password = $_POST ['password'];

        $query = "SELECT password from users where username = '$username'";
        $result = mysql_query ( $query ) or die ( "There was an error executing the query $query:<br/>" . mysql_error () );
        $row = mysql_fetch_array ( $result );
        $encrypted_pass = $row [0];

        if (crypt ( $password, $encrypted_pass ) == $encrypted_pass) {
            $flag = "y";
            header ( 'Location: m_a2_view.php?flag=y' );
        }

    } else {
        echo global_header ( "LOGIN" );
        login ();
    }

    validation ();
    page_footer ();

    function global_header($page_title) {
        $content = '<?xml version="1.0" encoding="UTF-8"?>
<!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-US" lang="en-US">
    <head>
        <title><?php echo "MARK - ' . $page_title . '</title>
        <link rel="stylesheet" href="style.css" />
    </head> 
    <body>
    <div id="container">
    <div id="logo"></div>';

        return $content;
    }

Upvotes: 1

Ry-
Ry-

Reputation: 224886

The error is caused by there already being output before the redirection. What you can do is buffer said output if you'd really like to keep things in that order with that coding style:

<?php ob_start(); ?>
<?php include('m_a2_functions.php'); ?>
<?php global_header(LOGIN); ?>
<?php login(); ?>
<?php 
if(isset($_POST['submit']))
{
    connect();
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT password from users where username = '$username'";
    $result = mysql_query($query) or die ("There was an error executing the query $query:<br/>".mysql_error());
    $row = mysql_fetch_array($result);
    $encrypted_pass = $row[0];

    if (crypt($password, $encrypted_pass) == $encrypted_pass) {
        $flag = "y";
        header('Location: m_a2_view.php?flag=y');
        ob_end_clean(); // Discard the buffer
    } else {
        ob_end_flush(); // Display the contents of the buffer
    }
}
?>
<?php validation(); ?>
<?php page_footer(); ?>

Upvotes: 2

Related Questions