2013-08-31 106 views
1

我正在努力將cookie添加到我現有的登錄表單中。 這是我到目前爲止有:要記住上次登錄名的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 frome。

回答

0

這段代碼如此糟糕......但解決問題的方法非常簡單。

按照此程序: - 當用戶登錄(Alredy驗證 - >檢查憑證後)使用easly命令setCookie(cookie_name,cookie_value,cookie_time,path)設置cookie;對於路徑值,我建議你使用「/」。 - 在您的登錄頁面重定向之前,如果用戶未通過會話值進行身份驗證,則必須檢查cookie是否已被isset(如果isset - >您可以轉到「Restricted Area」,則必須重定向到登錄頁面) 。

Marco