1

我正在努力将 cookie 添加到我现有的登录表单中。这是我到目前为止所拥有的:

登录.php

<?php require_once('../../Connections/connBoekengidsv2.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",        $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
 }
 return $theValue;
 }
 }

 // Hash Password field
if (isset($_POST['paswoord']) && $_POST['paswoord'] <> ""){$_POST['paswoord'] =  md5($_POST['paswoord']);}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();


$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['login'])) {
$loginUsername=$_POST['login'];
$password=$_POST['paswoord'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "index.php";
$MM_redirectLoginFailed = "login.php?error=Foutieve+login";
$MM_redirecttoReferrer = true;
mysql_select_db($database_connBoekengidsv2, $connBoekengidsv2);


$LoginRS__query=sprintf("SELECT login, paswoord FROM tbl_login_boekengids WHERE login=%s AND paswoord=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

$LoginRS = mysql_query($LoginRS__query, $connBoekengidsv2) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
 $loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else   {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;       

if (isset($_SESSION['PrevUrl']) && true) {
  $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
<!doctype html>
<html><!-- InstanceBegin template="/Templates/basis.dwt.php"     codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Boekengids: login</title>
<!-- InstanceEndEditable -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="../../jQuery/jquery.min.js"></script>
<script src="../../jQuery/jquery.formalize.min.js"></script>
<script src="../../jQuery/les.js"></script>
<link href="../../style.css" rel="stylesheet" type="text/css">
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div id="outline">
<header id="header">
<h1>Boekengids</h1>
</header>
<nav id="navigatie">
<a href="../../site_ontwerp/index.php">Overzicht boeken</a> |
<?php if ($_SESSION['MM_Username']) { ?>
<a href="../../site_ontwerp/logout.php">Logout</a>
<?php } else { ?>
<a href="../../site_ontwerp/admin/login.php">Login</a>
<?php } ?>
</nav>
<div id="data"> <!-- InstanceBeginEditable name="data" -->
<h1>Admin login</h1>
<p class="error"><?php echo $_GET['error']; ?></p>
<form action="<?php echo $loginFormAction; ?>" name="loginForm" id="loginForm" method="POST">
  <p>
    <label for="login">Login:</label>
    <input type="text" required name="login" id="login" value="<?php echo  $_COOKIE['mijnLogin'] ?>">
  </p>
  <p>
    <label for="paswoord">Paswoord:</label>
    <input type="password" required name="paswoord" id="paswoord">
  </p>
  <p>
    <label></label>
    <input name="Submit" type="submit" value="Login">
  </p>
</form>

我读到您无法在同一页面上读取 cookie。它只能在下一页(index.php)上完成。这是我编写 setcookie() 代码行的地方。

索引.php

 <?php setcookie('mijnLogin', $_POST['login'], time()+86400*30)?>
 <?php
 if (!isset($_SESSION)) {
 session_start();
 }
 $MM_authorizedUsers = "";
 $MM_donotCheckaccess = "true";

 // *** Restrict Access To Page: Grant or deny access to this page
 function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
 // For security, start by assuming the visitor is NOT authorized. 
 $isValid = False; 

 // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
// Therefore, we know that a user is NOT logged in if that Session variable is blank. 
if (!empty($UserName)) { 
// Besides being logged in, you may restrict access to only certain users based on an   ID established when they login. 
// Parse the strings into arrays. 
$arrUsers = Explode(",", $strUsers); 
$arrGroups = Explode(",", $strGroups); 
if (in_array($UserName, $arrUsers)) { 
  $isValid = true; 
} 
// Or, you may restrict access to only certain users based on their username. 
if (in_array($UserGroup, $arrGroups)) { 
  $isValid = true; 
} 
if (($strUsers == "") && true) { 
  $isValid = true; 
} 
 } 
 return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers,    $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" .urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo); 
exit;
}
?>
<!doctype html>
<html><!-- InstanceBegin template="

所有这些代码都归因于我添加的服务器行为。我现在想实现一个cookie,这样第二次有人想登录时,用户名就已经填好了。我尝试了几种尝试添加它的方法,但都没有奏效。当它测试它时,cookie 不会被创建。如果我更改 cookie 的名称以读取现有的 cookie,它确实有效。因此,登录代码的某个地方会阻止创建 cookie。

4

1 回答 1

0

这段代码太乱了……但是解决问题的方法非常简单。

遵循以下程序: - 当用户登录时(已通过身份验证 -> 检查凭据后)使用轻松命令 setCookie(cookie_name, cookie_value, cookie_time, path) 设置 cookie;对于路径值,我建议您使用“/”。- 在您的登录页面重定向之前,如果用户未通过会话值进行身份验证,您必须检查 cookie 是否已经 isset(如果 isset -> 您可以继续“限制区域”,而不是您必须重定向到登录页面)。

马可

于 2013-09-04T18:10:19.813 回答