我先建立了个页面,用户登录,中间需要进行用户名和密码登陆,同时希望当登陆正确的话,则建立cookie,否则不建立cookie。请问要怎么弄?登陆页面如下
/*此处建立cookie没有生一点效    <?php
session_start();
if ($_GET[out])
{
unset($_SESSION[user]);
unset($_SESSION[password]);
}
if($_GET[user]&&$_GET[pw])
{
$_SESSION[user]=$_GET[user];
$_SESSION[password]=$_GET[pw];
}
if($_SESSION[user]&&$_SESSION[password])
{
echo "登陆成功<br>用户名:".$_SESSION[user]."<br>密 码:".$_SESSION[password];
echo "<br><a href='new.php?out=out'>退出</a>";
}
?>    */
<html>
<head>
<title>ajax</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <script language="javascript">
  function showhint()
  {   var str1=document.form.user.value;
      var str2=document.form.pw.value;
  var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (str1.length<1)
  {   alert ("no user");
  return false;}
  if (str2.length<1)
  {
  alert ("no password");
  return false;
  }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4&&xmlhttp.status==200)
  { 
 if(xmlhttp.responseText == '0'){alert("用户名不存在或密码错误!");}else{
 document.getElementById("txt").innerHTML=xmlhttp.responseText;}
}   
  }
  xmlhttp.open("GET","intosql2.php?q="+str1+"&w="+str2,true);//alert("intosql2.php?q="+str1+"&w="+str2);
  xmlhttp.send();
  }
  </script>
</head>
<body><span id="txt">
<form name="form" method="post" >
<table width="200" border="0" cellspacing="0" cellpadding="0" bordercolor="red">
      <tr>
        <td width="117">用户名:
            <input type="text" name="user" maxlength="20" size="15">        </td>
        <td width="46" rowspan="2">
          <input name="submit" type="button" onclick="showhint()" style="font-size:24px;height:50px;width:70px" value="登陆">        </td>
      </tr>
      <tr>
        <td>密码:
            <input type="password" name="pw" maxlength="20" size="15">        </td>
        </tr>
      <tr>
        <td colspan="2"><a href="pw_out.php">忘记密码?</a>  <a href="register.php">注册新用户</a></td>
        </tr>
    </table>
</form></span>
</body>
</html>后台验证页面intosql2.php<?php
header("content-type:text/html; charset=gb2312");
$con = mysql_connect("localhost","root","");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }mysql_select_db("clint", $con);
  $sql=mysql_query("select * from clint where name='$_GET[q]'");
$row=mysql_fetch_array($sql);
if ($row[password]==$_GET["w"])
  {
  echo "<font size=2 color=green>login ok!</font>";
  }
  else
  {
 echo '0' ;
  }
  mysql_close($con);
  function gbkToUtf8 ($value) {
return iconv("gb2312", "UTF-8", $value);
}
?>

解决方案 »

  1.   

    $_SESSION[user]改为$_SESSION["user"]
    其他类似
      

  2.   

    你表单用的是POST方法,把$_GET改成$_POST就OK了
    还有你的这是session而不是cookie,汗一个先
      

  3.   

    说的也是,改了。至于标题嘛,当时是一起用的,我只是没有把cookie弄进来,题目却取的那个,呵呵。我这个是弄的差不多了。但是每次刷新的时候就有点问题了,每次刷新似乎都提交了一次表单,弄得那个提示框总是弹出来,这个问题该怎么解决呀?