English 中文(简体)
创建网站
原标题:Creating cookies in php

我正试图学习如何使用PHPNerds。 我在管理他们提到的文字时遇到麻烦(我几乎理解该守则的内容,但我无法拿到哪一部法典将存放在哪部名称上)。 页: 1

User Login

<html>
<head>
<title>User Logon</title>
</head>
<body>
  <h2>User Login </h2>
  <form name="login" method="post" action="login.php">
   Username: <input type="text" name="username"><br>
   Password: <input type="password" name="password"><br>
   Remember Me: <input type="checkbox" name="rememberme" value="1"><br>
   <input type="submit" name="submit" value="Login!">
  </form>
</body>
</html>

Login Code

<?php
/* These are our valid username and passwords */
$user =  jonny4 ;
$pass =  delafoo ;

if (isset($_POST[ username ]) && isset($_POST[ password )) {

    if (($_POST[ username ] == $user) && ($_POST[ password ] == $pass)) {    

        if (isset($_POST[ rememberme ])) {
            /* Set cookie to last 1 year */
            setcookie( username , $_POST[ username ], time()+60*60*24*365,  /account ,  www.example.com );
            setcookie( password , md5($_POST[ password ]), time()+60*60*24*365,  /account ,  www.example.com );

        } else {
            /* Cookie expires when browser closes */
            setcookie( username , $_POST[ username ], false,  /account ,  www.example.com );
            setcookie( password , md5($_POST[ password ]), false,  /account ,  www.example.com );
        }
        header( Location: index.php );

    } else {
        echo  Username/Password Invalid ;
    }

} else {
    echo  You must supply a username and password. ;
}
?>

Validating

<?php
/* These are our valid username and passwords */
$user =  jonny4 ;
$pass =  delafoo ;

if (isset($_COOKIE[[ username ]) && isset($_COOKIE[ password )) {

    if (($_POST[ username ] != $user) || ($_POST[ password ] != md5($pass))) {    
        header( Location: login.html );
    } else {
        echo  Welcome back   . $_COOKIE[ username ];
    }

} else {
    header( Location: login.html );
}
?>

提前感谢。

问题回答

我现在收到这份资料。

购买力平价是灵活的。 你们要么把你的.html与你的逻辑分开,要么在一页内服务。 你们将就如何处理这一“适当”方法提出论据,但最终必须与你自己的选择以及你今后如何计划处理守则。

个人而言,在一项小项目上,我对一个档案中的标识页有逻辑和html。

标识:

<?php
/* These are our valid username and passwords */
$user =  jonny4 ;
$pass =  delafoo ;
$error = null;

if (isset($_POST[ username ]) && isset($_POST[ password )) {

    if (($_POST[ username ] == $user) && ($_POST[ password ] == $pass)) {    

        if (isset($_POST[ rememberme ])) {
            /* Set cookie to last 1 year */
            setcookie( username , $_POST[ username ], time()+60*60*24*365,  /account ,  www.example.com );
            setcookie( password , md5($_POST[ password ]), time()+60*60*24*365,  /account ,  www.example.com );

        } else {
            /* Cookie expires when browser closes */
            setcookie( username , $_POST[ username ], false,  /account ,  www.example.com );
            setcookie( password , md5($_POST[ password ]), false,  /account ,  www.example.com );
        }
        header( Location: index.php );
        exit;
    } else {
        $error =  Username/Password Invalid ;
    }

} else {
    $error =  You must supply a username and password. ;
}
?>
<html>
<head>
<title>User Logon</title>
</head>
<body>
  <h2>User Login </h2>
  <?php echo $error ? $error. <br>  :   ; ?>
  <form name="login" method="post" action="login.php">
   Username: <input type="text" name="username"><br>
   Password: <input type="password" name="password"><br>
   Remember Me: <input type="checkbox" name="rememberme" value="1"><br>
   <input type="submit" name="submit" value="Login!">
  </form>
</body>
</html>

指数:

<?php
/* These are our valid username and passwords */
$user =  jonny4 ;
$pass =  delafoo ;

if (isset($_COOKIE[[ username ]) && isset($_COOKIE[ password )) {

    if (($_POST[ username ] != $user) || ($_POST[ password ] != md5($pass))) {    
        header( Location: login.php );
        exit;
    } else {
        echo  Welcome back   . $_COOKIE[ username ];
    }

} else {
    header( Location: login.php );
    exit;
}
?>

如果你重,,我将看MVC(阅读控制器)和OOP(注重目标的方案拟定)看看它如何恰当。 但是,就基本情况而言,像这一例子那样,在处理原木方面没有任何特别错误。

从我的角度来看——为一家网络公司工作——在我继承一个新客户的项目时,我绝对会对此表示仇恨,而老的节目家把他们可能掌握的一切分割成新的档案。 “工作正确工具”这一术语也可适用于项目的基本方法。 在某些情况下,一个网站太小,通过一个框架或建立一个详细的档案系统开展工作将浪费大量时间。 所有这一切都取决于你们的需要,而你们的需求将随着经验而变得清楚。

可以肯定的是,凡是说在厨师中储存用户名和密码的人,都绝对是正确的。 通常,你做的是储存独一无二的身份证,并用一个数据库来吸引相关用户。 这样,你的数据就会被任何新冰 ha或从 desktop开始的idi子所劫持。

或许可以在验证页上打字,并将数值与非《持久性有机污染物行动计划》超级全球化的 co进行比较。

if (isset($_COOKIE[ username ],$_COOKIE[ password ])) {

    if ($_COOKIE[ username ] == $user && $_COOKIE[ password ] == md5($pass)) {    
        echo  Welcome back   . $_COOKIE[ username ];
    } else {
         header( Location: login.html );        
    }
} else {
    header( Location: login.html );
}

这是这样做的另一个方式,试图这样做。

<?php
/* These are our valid username and passwords */
$user =  jonny4 ;
$pass =  delafoo ;

if (isset($_COOKIE[[ username ]) && isset($_COOKIE[ password )) {

   if (($_POST[ username ] != $user) || ($_POST[ password ] != md5($pass))) {    
    header( Location: login.php );
    exit;
   } else {
    echo  Welcome back   . $_COOKIE[ username ];
  }

 } else {
   header( Location: login.php );
   exit;
   }
  ?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签