Home > Backend Development > PHP Tutorial > 菜鸟写的留言板登陆代码各位大神帮我看看,为什么要点击登陆两次才能登陆,为什么要点击两次退出才能退出?

菜鸟写的留言板登陆代码各位大神帮我看看,为什么要点击登陆两次才能登陆,为什么要点击两次退出才能退出?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 14:16:15
Original
1262 people have browsed it

PHP 菜鸟写的代码

菜鸟写的留言板登陆代码各位大神帮我看看,为什么要点击登陆两次才能登陆,为什么要点击两次退出才能退出?问题出在哪里,大神帮我看看,先谢谢你了。代码如下:
<?phpinclude("Sql.php");if ($_GET[out]){  setcookie("cookie","out");  echo "<script language=\"javascript\">localhost.href='login.php';</script>";}if ($_POST[id]=='xxx')  {   $pw=md5($_POST[pw]);   if($pw=='e1bfd762321e409cee4ac0b6e841963c')       {         setcookie("cookie","ok");          echo "<script language=\"javascript\">localhost.href='login.php';</script>";       }  }include("Head.php");if ($_COOKIE['cookie']!='ok'){?><SCRIPT language=javascript>function Checklogin()  {     if (myform.id.value=="")     {        alert ("请填写登陆名");        myform.id.focus();        return false;     }      if (myform.pw.value=="")     {        alert ("密码不能为空");        myform.pw.focus();        return false;     }  }</SCRIPT> <form action="" method="post" name="myform" onsubmit="return Checklogin();">  ID:<input type="text" name="id" ><br>  PW:<input type="password" name="pw"> <input type="submit" name="submit" value="登陆" > </form><?}else{?>  <a href='?out=login'>退出</a><?}?>
Copy after login


回复讨论(解决方案)

不知错哪了,望大神指点迷经啊

在纠正了“变量未定义”的警告后,未发现你说的现象
但不能排除是 sql.php 或 head.php 中的问题

贴出你include的文件看下、看是否异样。

贴出你include的文件看下、看是否异样
Sql.php文件代码:

<?php/*CREATE TABLE `message` (  `id` tinyint(1) NOT NULL auto_increment,  `user` varchar(25) NOT NULL,  `title` varchar(50) NOT NULL,  `content` tinytext NOT NULL,  `lastdate` date NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;*/$conn=mysql_connect("localhost","root","") or die ("数据库错误");mysql_select_db("bbs",$conn);mysql_query("set names 'GBK'");  //中文编码function htmtocode($content) {    $content = str_replace("\n","<br>",str_replace(" "," ",$content));       return $content; }//把输入的空格跟换行用str_replace("\n","<br>",str_replace(" "," ",$content))替换成PHP能接受的格式,实现输入什么格式就显示什么格式.?>
Copy after login

Head.php文件代码:
<head><meta http-equiv="Content-Type" content="text/html";charset="gb2312"><title></title><link href="images/css.css" rel="stylesheet" type="text/css"><b><a href="Textbook.php">添加留言</a> | <a href="List.php">浏览留言</a> | <a href="login.php">登陆</a> </b><hr size=1>
Copy after login

Textbook.php文件代码:
<?phpinclude("Sql.php");//if ($_POST['submit'])if (isset($_POST['submit'])) //注一定用(isset($_POST['submit']))因为它是判断 submit的数据提交没有.{     $sql="insert into message(id,user,title,content,lastdate)values('','$_POST[user]','$_POST[title]','$_POST[content]',now())";     mysql_query($sql);     echo "<script language=\"javascript\">alert('添加成功');history.go(-1)</script>";   }include("Head.php");?><SCRIPT language=javascript>function CheckPost(){    if (myform.user.value=="")    {      alert("用户名不能这空,请填写用户名");      myform.user.focus();      return false;    }    if (myform.title.value.length<5)    {      alert("标题不能少于5个字符");      myform.title.focus();      return false;    }    if (myform.content.value.length<8)    {       alert("内容不能少于8个字节");       myform.content.focus();       return false;    }     }</SCRIPT><form action="Textbook.php" method="post" name="myform" onsubmit="return CheckPost();"> <!--注:这里的action="Textbook.php"是定就是当前的文件名-->用户:<input type="text" size="10" name="user"><br>标题:<input type="text" name="title"><br>留言内容:<br><textarea name="content" rows=20 cols=30></textarea><br><input type="submit" name="submit" value="提交留言"></form>
Copy after login

List.php文件代码:
<?include("Sql.php");include("Head.php"); $sql="SELECT * from message order by id desc";  //order by id desc 倒序排列  $query=mysql_query($sql); while($row=mysql_fetch_array($query)){?><table width=500 border="1" align="center" cellpadding="5" cellspacing="2" bgcolor="#add3ef"><tr bgcolor="#eff3ff"><td bgcolor ="eee3ff">ID序列:<?=$row[id]?></td> <td bgcolor ="eff3ff">标题:<?=$row[title]?></td> <td bgcolor ="#aeeffc">用户:<?=$row[user]?></td> <td bgcolor ="#ffffff">内容:<? echo htmtocode($row[content]);?></td></tr></table><? }?>
Copy after login

在纠正了“变量未定义”的警告后,未发现你说的现象
但不能排除是 sql.php 或 head.php 中的问题 
变量未定义是不是指的这里 

setcookie("cookie","out");
改成 setcookie("cookie","out",time()-3600);

-------------
setcookie("cookie","ok");
改成 
setcookie("cookie","ok",time()+3600);

setcookie("cookie","out");
改成 setcookie("cookie","out",time()-3600);

-------------
setcookie("cookie","ok");
改成 
setcookie("cookie","ok",time()+3600); 
 我改了还是要点击两次才能登陆,要点击两次才能退出.
setcookie("cookie","out",time()-3600);
  echo "<script>localhost.href='login.php';</script>";
}
if ($_POST[id]=='xxx')
  {
   $pw=md5($_POST[pw]);
   if($pw=='e1bfd762321e409cee4ac0b6e841963c')
       {
         setcookie("cookie","ok",time()+3600);
          echo "<script>localhost.href='login.php';</script>";
       }
  }

localhost.href='login.php';

不是应该是 location.href=么

所有的localhost都应该是location

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template