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() コード行を書いた場所です。

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="

このコードはすべて、私が追加した Server Behavior に属しています。誰かが 2 回目にログインしようとすると、ユーザー名が既に入力されているように、Cookie を実装したいと考えています。追加しようといくつかの方法を試しましたが、うまくいきませんでした。テストすると、Cookie は作成されません。Cookie の名前を既存の Cookie を読み取るように変更すると、機能します。そのため、どこかでログイン コードが Cookie の作成を妨げています。

4

1 に答える 1

0

このコードはめちゃくちゃです...しかし、問題を解決する方法は非常に簡単です。

次の手順に従います。 - ユーザーがログインしたとき (認証済み -> 資格情報の確認後) 簡単なコマンド setCookie(cookie_name, cookie_value, cookie_time, path) で Cookie を設定します。パス値には「/」を使用することをお勧めします。- ログインページをリダイレクトする前に、ユーザーがセッション値で認証されていない場合は、Cookie が既に設定されているかどうかを確認する必要があります (設定されている場合 -> 「制限領域」に進むことができますが、代わりにログインページにリダイレクトする必要があります)。

マルコ

于 2013-09-04T18:10:19.813 に答える