批改状态:合格
老师批语:你好, 请将问题发到群中更好


<?phpdefine('DB_HOST','localhost');define('DB_USER','phpcnyizhi');define('DB_PWD','phpcnyizhi');define('DB_NAME','phpcnyizhi');define('DB_PORT','3306');define('DB_TYPE','mysql');define('DB_CHARSET','utf8');define('DB_DSN',DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET.";port=".DB_PORT);//定义PDO的DSN,数据源名,包括主机名,端口号和数据库名。?>
<?phpif ($_GET["action"] == "logout"){setcookie("username","",time()-3600);setcookie("auth","",time()-3600);exit(" <script>alert('退出成功');location.href='login.php';</script>");}if(isset($_COOKIE['username']) && $_COOKIE['auth']){exit("<script>alert('您已经登陆');location.href='index.php';</script>");}?><style>form{border: 1px solid #ccc; width: 300px; margin: 20px auto; text-align: center;}label{display: block; margin: 20px 0;}</style><form action="check.php" method="post"><label>用户名:<input type="text" name="username" placeholder="请输入用户名" require></label><label>密码:<input type="password" name="password" placeholder="请输入密码" require</label><label>七天自动登陆:<input type="checkbox" name="autologin" value="1"></label><label><input type="submit" name="submit"></label></form>
<?phpheader('content-type:text/html;charset=utf-8');$username=$_POST['username'];$password=md5($_POST['password']);$autologin=$_POST['autologin'];require('datebase.php');try{//数据库连接,得到一个PDO对象 不要把所有的代码写须Try中,非常消耗性能。$pdo=new pdo(DB_DSN,DB_USER,DB_PWD,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING,]);}//捕捉特定于数据库信息的PDOException异常catch(PDOException $e){echo $e->getMessage();}//捕捉拥有Throwable接口的错误或者其它异常catch(Throwable $e){echo $e->getMessage();}$sql="SELECT `username`,`password`,`id` FROM `yizhi_user` WHERE `username`=? AND `password`=?";$stmt=$pdo->prepare($sql);$stmt->bindParam(1,$username);$stmt->bindParam(2,$password);$stmt->execute();$res=$stmt->fetch(PDO::FETCH_ASSOC);if($stmt->rowCount()==1){//新登陆的时候,把其它用户Cookies信息删除了setcookie("username","",time()-3600);setcookie("auth","",time()-3600);//选择了记住密码if(!empty($autologin)){setcookie("username",$username,strtotime("+7 days"));$salt="yizhi"; //加混淆符号$auth=md5($username.$password.$salt).','.$res['id'];setcookie("auth",$auth,strtotime("+7 days"));}else{setcookie("username",$username);$salt="yizhi"; //加混淆符号$auth=md5($username.$password.$salt).','.$res['id'];setcookie("auth",$auth);}exit("<script>alert('登陆成功');location.href='index.php';</script>");}else{exit("<script>alert('用户名或者密码错误');location.href='login.php';</script>");}?>
<?phpheader('content-type:text/html;charset=utf-8');require('datebase.php');try{$pdo=new pdo(DB_DSN,DB_USER,DB_PWD,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING,]);}//捕捉特定于数据库信息的PDOException异常catch(PDOException $e){echo $e->getMessage();}//捕捉拥有Throwable接口的错误或者其它异常catch(Throwable $e){echo $e->getMessage();}//从Cookie中取出auth$auth=$_COOKIE['auth'];$res=explode(',',$auth);$id=end($res) ;$auth_rel=$res[0];$sql="SELECT `username`,`password`,`id` FROM `yizhi_user` WHERE `id`=?";$stmt=$pdo->prepare($sql);$stmt->bindParam(1,$id);$stmt->execute();$res=$stmt->fetch(PDO::FETCH_ASSOC);if($stmt->rowCount()==1){$username=$res['username'];$password=$res['password'];$salt="yizhi";$auth=md5($username.$password.$salt);if($auth!==$auth_rel){exit("<script>alert('请重新登陆');location.href='login.php';</script>");}}else{exit("<script>alert('请重新登陆');location.href='login.php';</script>");}?>欢迎你,<?php echo $_COOKIE['username']?>登陆成功。<br><a href="login.php?action=logout">退出登陆</a>
如果要判断一个变量被设置并且不为空,可以先使用isset,然后使用empty
问题:上面的说法,是否正确?
问题:if($a) 相当于那个函数的功能?
麻烦老师依次回答这三个问题,非常感谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号