Cookie记住上次登录名

时间:2013-08-31 17:24:04

标签: php cookies

我很难在现有的登录表单中添加Cookie。 这就是我到目前为止所做的:

的login.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()代码行的地方。

的index.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被创建。

1 个答案:

答案 0 :(得分:0)

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

请按照以下步骤操作: - 当用户登录(Alredy authenticated - &gt;检查凭据后)使用easly命令setCookie(cookie_name,cookie_value,cookie_time,path)设置cookie;对于路径值我建议你使用“/”。 - 在您的登录页面重定向之前,如果用户未通过会话值进行身份验证,您必须检查cookie是否为alredy isset(如果是isset - >,您可以将其转到“受限区域”,而是必须重定向到登录页面)

Marco