PHP重定向到另一个页面并传递值?

时间:2012-04-10 00:35:54

标签: php

我有一个问题,我需要重定向到另一个页面并传递一个值,这很容易,我知道该怎么做,除了旧标题问题:无法修改标题信息 - 已经发送的标题......第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(); ?>

这很好,除了标题部分有问题。它不会重定向。

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

第17行

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

我知道这是造成它的原因。它在我的页面开头加载global_header,然后无法重定向到另一个页面。当我点击提交时,我的页面应该重定向,但它不能!

即使我开始删除东西,它也行不通。完全摆脱global_header()之后,它将永远工作,这是我无法做到的。

2 个答案:

答案 0 :(得分:2)

错误是由重定向之前已经输出引起的。如果你真的想用这种编码风格保持顺序,你可以做的就是缓冲输出:

<?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(); ?>

答案 1 :(得分:1)

您遇到此问题的原因是您的脚本是多个开放<?php并且?> PHP的{​​{1}}标记之间有spacenew line他们......

替换

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

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

你的剧本

    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;
    }