2

些細な質問で申し訳ありませんが、header() php 関数を使用してページにリダイレクトする際に問題が発生しています。より具体的には、ユーザーが存在しないプロファイル ページを表示しようとしたときに、ユーザーをリダイレクトするのに苦労しています。私の問題は、セッションの開始と、基本的なヘッダーを表示するための html を含むヘッダー ファイルを常に含めていることです。これは、header() 関数を使用して、このヘッダー ファイルを含むスクリプト内のページにリダイレクトできないということですか? この問題を回避する 1 つの方法は、ヘッダーの html 部分を別のファイルに分割し、最初にヘッダー スクリプトをインクルードし、次にプロファイル スクリプトを記述し、最後にヘッダーの html 部分をインクルードすることではないかと考えました。それは悪い習慣ですか?profile.php スクリプトは次のとおりです。

<?php include("inc/incfiles/header.inc.php"); ?>
<?php

if(isset($_GET['u'])) {

    //check user exists

    $username = mysql_real_escape_string($_GET['u']);

    if (ctype_alnum($username)) {

        $check = mysql_query("SELECT username, email FROM users WHERE username = '$username'");

        if (mysql_num_rows($check)===1) {

            $get = mysql_fetch_assoc($check); //execute query and store in array

            $username = $get['username'];

            $email = $get['email'];

        }

        else {
            header("Location: index.php");

            die();

        }

    }

    else {

        echo "username has to be alphanumeric";

    }

}

else {

    echo "error";

}

?>


<h2> Profile page of <?php echo "$username";?>


<h3> Email: <?php echo "$email";?>

header.inc.php ファイル:

<?php

include ("inc/scripts/mysql_connect.inc.php");

//start the session


session_start();

//Checks whether the user is logged in

$user = $_SESSION["user_login"];

if (!isset($SESSION["user_login"])) {

//header("Location: index.php");

//exit();


}

else

{

    header("location: home.php");

}


?>

<?php


//Login Scripts has to be at the top to make sure header() redirecting works


if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {

$user_login = preg_replace('#[^A-Za-z0-9]#i','', $_POST["user_login"]); //filter user login text

$password_login = preg_replace('#[^A-Za-z0-9]#i','', $_POST["password_login"]); //filter user password text

$md5password_login = md5($password_login);

$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND 
password='$md5password_login' LIMIT 1"); //query the user


//Check for user's existence

$userCount = mysql_num_rows($sql); //count number of rows

if ($userCount == 1) {

    while ($row = mysql_fetch_array($sql)) {

        $id = $row["id"];

    }


    $_SESSION["user_login"] = $user_login;

    $_SESSION["password_login"] = $md5password_login;



header("Location: home.php");


    exit();


}

else {

    echo "That information is incorrect, try again";

}

}



?>



<html>

<head>

    <link href = "css/main.css" rel = "stylesheet" type = "text/css">

    <title> title </title>

</head>

<body>

    <div class = "wrapper">

        <div id = "header">

            <div class = "logo">

                <img src = "img/Logo.png">

        </div>

            <div id = "login-header">


                    <form action = "index.php" method ="post" name = "form1" id = "form1">

                            <div class = "input-wrapper"><input type = "text" size = "25" name = "user_login" id = "user_login" placeholder = ">Username" ></div>

                            <div class = "input-wrapper"><input type = "password" size = "25" name = "password_login" id = "password_login" placeholder = "Password" ></div>

                            <div class = "input-wrapper"><input type = "submit" name = "login" value = "Sign in"></div>

                    </form>


            </div>


        <div id = "menu">

            <a href = "#"></a>

            <a href = "#"></a>

        </div>

    </div>

</div>
4

1 に答える 1

4

好きな場所にファイルを含めることができます。問題はOUTPUTです。通話を行う場合、通話の前に出力header()を実行することはできません。インクルードが単に出力を吐き出すだけで、後で使用するために関数/変数を定義するだけではない場合は、その出力を行わないようにインクルードを変​​更するか、少なくとも後で出力をバッファリング/延期する必要があります。例えば

<?php

include('some_file_that_causes_output_when_loaded.php');
header('This header will not work');

インクルードが出力を行い、ヘッダー呼び出しを強制終了するため、何があっても失敗します。しかし、インクルードを変​​更すると、次のようになります。

<?php

include('file_that_just_defines_functions.php');
header('This header will work');
function_from_include_that_causes_output();

うまくいきます。コードを変更できない場合は、

<?php

ob_start();
include('some_file_that_causes_output_when_loaded.php');
header('This header will still work, because we're buffering output');
ob_end_clean();     
于 2013-08-09T18:12:11.327 に答える