0

I have an error on line 2 and 3, that says undefined index: username,undefined index: password, can you help me about this? All find, set a cookie is ok but I got a display error in the pageSee 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>
maazza
  • 7,016
  • 15
  • 63
  • 96
  • You need to check that the value is set before using it. You are open to SQL injections – chris85 Dec 19 '15 at 00:00
  • do you have a form-tag before that? what method have you specified, `POST` or `GET`? – Jeff Dec 19 '15 at 00:14
  • I think you shouldn't set a raw password as cookie value. Also, @Jeff is right, where is your form tag? There's no need to wrap the variables with double quotes inside `setcookie()`. Don't use `mysql_*`, it's deprecated, use `mysqli_*` instead! Lastly, I would set `$cookie_name` and `$cookie_value` inside the `if(isset($_POST['username']))` statement. – Zeke Dec 19 '15 at 00:48

1 Answers1

0

You need to add the form tag. If you don't add it, you are not sending data.

    <?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>
Odei Alba
  • 1
  • 2