如何使用php文本框设置cookie

时间:2015-12-18 23:56:51

标签: php

我在第2行和第3行有一个错误,它表示未定义索引:用户名,未定义索引:密码,你可以帮我解决这个问题吗? 所有找到,设置一个cookie都没关系,但我在页面See the picture

中出现显示错误
    <?php
$cookie_name = $_POST['Username'];
$cookie_value =$_POST['password'];
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "try";

mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database);

if (isset($_POST['username'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
        $sql = "SELECT * FROM user WHERE usern='".$username."' AND userp='".$password."' LIMIT 1";
        $res = mysql_query($sql);
        if (mysql_num_rows($res) == 1) {
            if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie("$cookie_name", "$cookie_value", time()+60*60*24*365, "/");

        } else {
        }
        header('Location: index.php');

            exit();

        } else {
            echo "<script type='text/javascript'>alert('Invalid Username or Password or Account not Registered!')</script>";
            echo "<script> window.location.assign('index1.php'); </script>";
            exit();
        }
}
?>
<input type="text" value="" placeholder="Username" name="username"/>
    <input type="password" value="" placeholder="Password" name="password"/>
    Remember Me: <input type="checkbox" name="rememberme" value="1">
    <button><input type="submit" name="submit" value="Sign In"/></button><br><br>

1 个答案:

答案 0 :(得分:0)

您需要添加表单标记。如果您不添加,则不会发送数据。

    <?php
if (isset($_POST['submit'])){
    $cookie_name = $_POST['Username'];
    $cookie_value =$_POST['password'];
    $mysql_host = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "try";

    mysql_connect($mysql_host, $mysql_user, $mysql_password);
    mysql_select_db($mysql_database);

    if (isset($_POST['username'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
            $sql = "SELECT * FROM user WHERE usern='".$username."' AND userp='".$password."' LIMIT 1";
            $res = mysql_query($sql);
            if (mysql_num_rows($res) == 1) {
                if (isset($_POST['rememberme'])) {
                /* Set cookie to last 1 year */
                setcookie("$cookie_name", "$cookie_value", time()+60*60*24*365, "/");

            } else {
            }
            header('Location: index.php');

                exit();

            } else {
                echo "<script type='text/javascript'>alert('Invalid Username or Password or Account not Registered!')</script>";
                echo "<script> window.location.assign('index1.php'); </script>";
                exit();
            }
    }
}
    ?>
    <form action='' method='post'>
    <input type="text" value="" placeholder="Username" name="username"/>
        <input type="password" value="" placeholder="Password" name="password"/>
        Remember Me: <input type="checkbox" name="rememberme" value="1">
      <input type="submit" name="submit" value="Sign In"/>
    </form><br><br>