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

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><?}?>

回复讨论(解决方案)

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

在纠正了“变量未定义”的警告后,未发现你说的现象
但不能排除是 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能接受的格式,实现输入什么格式就显示什么格式.?>

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>

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>

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><? }?>

在纠正了“变量未定义”的警告后,未发现你说的现象
但不能排除是 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

Statement
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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor