4

PHP ページから別のページにどのようにリダイレクトしますか?

</head>

<body>
    <!-- Home -->
    <div data-role="page" id="page1">
        <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
            <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d" class="ui-btn-right">
                     Home  
                    </a>
            <h3>
                Login Process
            </h3>
        </div>
        <div data-role="content">

            <?php
    // takes the variables from action script and assigns them php variables names
    $user = $_POST ['username'];
    $pass = $_POST ['password'];

    // if there is a user name and password
    if ($user && $pass)
    {
        // connect to server
        mysql_connect("localhost", "", "") or die(mysql_error());
        //select database
        mysql_select_db("") or die(mysql_error());

        //Create a query that selects all data from the PATIENT table where the username and password match
        $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";

        //executes query on the database
        $result = mysql_query ($query) or die ("didn't query");
        //this selects the results as rows

        $num = mysql_num_rows ($result);
        //if there is only 1 result returned than the data is ok 
        if ($num == 1)
        {
            //sends back a data of "Success"
            echo "Successful Login";
            $row=mysql_fetch_array($result);
            $_SESSION['Name'] = $row['Name'];
            $_SESSION['Address'] = $row['Address'];
        }
        else
        {
            //sends back a message of "failed"
            echo "Unsuccessful Login";
        }
    }

    ?>
        </div>
    </div>
</body>

</html>

そのため、ユーザーがログインすると、上記のページに移動します。私がする必要があるのは、ログインが成功した場合、別の PHP ページにリダイレクトする必要があるということです。


login.php

<?php
    // takes the variables from action script and assigns them php variables names
    $user = $_POST ['username'];
    $pass = $_POST ['password'];
    $error = '';

    // if there is a user name and password
    if ($user && $pass)
    {
        // connect to server
        mysql_connect("localhost", "", "") or die(mysql_error());
        //select database
        mysql_select_db("") or die(mysql_error());

        //Create a query that selects all data from the PATIENT table where the username and password match
        $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";

        //executes query on the database
        $result = mysql_query ($query) or die ("didn't query");
        //this selects the results as rows

        $num = mysql_num_rows ($result);
        //if there is only 1 result returned than the data is ok 
       if ($num == 1)
    {
    header("Location: http://helios.hud.ac.uk/u101010/PHP/details1.php");
    }
            //sends back a data of "Success"
            $return_message =  "Successful Login";
            $row=mysql_fetch_array($result);
            $_SESSION['Name'] = $row['Name'];
            $_SESSION['Address'] = $row['Address'];
        }
        else
        {
            //sends back a message of "failed"
            $return_message = echo "Unsuccessful Login";
        }
    }
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <meta name="apple-mobile-web-app-capable" content="yes" />
            <meta name="apple-mobile-web-app-status-bar-style" content="black" />
            <title>
            </title>
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
            <link rel="stylesheet" href="my.css" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
            </script>
            <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
            </script>
            <script src="my.js">
            </script>
            <!-- User-generated css -->
            <style>
            </style>
            <!-- User-generated js -->
            <script>
                try {

        $(function() {

        });

      } catch (error) {
        console.error("Your javascript has an error: " + error);
      }
            </script>

    ?>
    </head>

    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d" class="ui-btn-right">
                     Home  
                    </a>
                <h3>
                    Login Process
                </h3>
            </div>
            <div data-role="content">

                <?php echo $return_message; ?>

            </div>
        </div>
    </body>

    </html>

詳細1.php

<!DOCTYPE html>
<?php
    session_start();
    ?>
    <html>

    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

                $(function() {

                });

            } catch (error) {
                console.error("Your javascript has an error: " + error);
            }
        </script>
    </head>

    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Main Menu
                        </a>
                <h3>
                    Your details
                </h3>


            </div>
            <div data-role="content">
                Name:
                <?php echo $_SESSION['Name'];?>
                <br /> Address:
                <?php echo $_SESSION['Address'];?>



            </div>
    </body>

    </html>

details.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="my.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
    </script>
    <script src="my.js">
    </script>
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
    <script>
        try {

            $(function() {

            });

        } catch (error) {
            console.error("Your javascript has an error: " + error);
        }
    </script>
</head>

<body>
    <!-- Home -->
    <div data-role="page" id="page1">
        <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Main Menu
                        </a>
            <h3>
                Your details
            </h3>
        </div>
        <form name="form1" method="post" action="details1.php">
            <strong>Details</strong>
            <br />
            <br /> Name: <input type="text" name "Name" />
            <br /> Address: <input type="text" name="Address" />
            <br />
            <input type="submit" name="Submit" value="Book appointment" />
            <br />
            <input type="submit" name="Submit" value="Cancel appointment" />
        </form>

    </div>
</body>

</html>

login.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="my.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
    </script>
    <script src="my.js">
    </script>
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
    <script>
        try {

            $(function() {

            });

        } catch (error) {
            console.error("Your javascript has an error: " + error);
        }
    </script>
</head>

<body>
    <!-- Home -->
    <div data-role="page" id="page1">
        <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
            <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d" class="ui-btn-right">
                     Home  
                    </a>
            <h3>
                Login
            </h3>
        </div>
        <div data-role="content">
            <h4>
                Enter login details below:
            </h4>

            <form name="form1" method="post" action="login.php">
                <strong>Patient Login </strong>
                <br />
                <br /> Username: <input name="username" type="text" id="username" />
                <br /> Password: <input name="password" type="password" id="password" />
                <br />
                <br />
                <br />
                <input type="submit" name="Submit" value="Login" />
            </form>
            <br />
            <div data-role="content">
                <h4>
                    Please ensure your username and password are kept secure.
                </h4>

                <br />
            </div>
        </div>
</body>

</html>
4

4 に答える 4

3

ヘッダーを変更してスクリプトを終了するだけです。

header("Location: http://www.example.com");
die();
于 2013-03-27T23:20:49.873 に答える
3

header()関数を次のように使用してみてください。

if ($num == 1)

{

header("Location: http://www.example.com");

}

明らかにhttp://www.example.comを選択した場所に変更し、ページ内の HTML の上に配置します。そうしないと、ヘッダーが既に設定されているというエラーが発生します。

上記を念頭に置いて、次のようにコードを再配置する必要があります。

    <?php

// takes the variables from action script and assigns them php variables names
$user = $_POST ['username'];
$pass = $_POST ['password'];
$error = '';

// if there is a user name and password
if ($user && $pass)
{
    // connect to server
    mysql_connect("localhost", "", "") or die(mysql_error());
    //select database
    mysql_select_db("") or die(mysql_error());

    //Create a query that selects all data from the PATIENT table where the username and password match
    $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1)
    {
        //sends back a data of "Success"
header("Location: success.php");

    }
    else
    {
        //sends back a message of "failed"
        $error = "Unsuccessful Login";
    }
}

?>

<html>
<head>
</head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Login Process
                </h3>
            </div>
            <div data-role="content">


<?php  echo $error; ?>

            </div>
        </div>
    </body>
</html>`

さらに、 SQL インジェクション攻撃にさらされているため、この種のことを進める前に、 PDOまたはmysqliとバインドされた値を持つ準備済みステートメントを確認することをお勧めします。

于 2013-03-27T22:40:33.507 に答える
2

JavaScript を使用できます

   <script type="text/javascript">window.location = 'PathToYourPage.extension'</script>
于 2013-03-27T22:44:14.713 に答える
2

使うだけ

header("Location: http://www.test.com");

以前に送信された出力がない場合、ヘッダーリダイレクトのみを設定できることに注意してください。単一の単語も、html 構文も、単純な空白もありません。

詳細:ヘッダー関数

そのため、スクリプトの先頭に PHP 部分を配置し、直接の「エコー」ではなく、成功/エラー メッセージを変数に格納する必要があります。

例: if 節の変数 "$return_message" に注意してください。「エコー」はもうありませんが、HTML コンテキストでは、出力のエコーが見つかります。

<?php
// takes the variables from action script and assigns them php variables names
$user = $_POST ['username'];
$pass = $_POST ['password'];

// if there is a user name and password
if ($user && $pass)
{
    // connect to server
    mysql_connect("localhost", "", "") or die(mysql_error());
    //select database
    mysql_select_db("") or die(mysql_error());

    //Create a query that selects all data from the PATIENT table where the username and password match
    $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1)
    {
        //sends back a data of "Success"
        $return_message =  "Successful Login";
        $row=mysql_fetch_array($result);
        $_SESSION['Name'] = $row['Name'];
        $_SESSION['Address'] = $row['Address'];
    }
    else
    {
        //sends back a message of "failed"
        $return_message = echo "Unsuccessful Login";
    }
}

?></head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Login Process
                </h3>
            </div>
            <div data-role="content">

<?php echo $return_message; ?>

            </div>
        </div>
    </body>
</html>
于 2013-03-27T22:42:48.187 に答える