再次感谢那些建议和帮助我的代码的人。不幸的是,一切正常,直到我清除缓存和cookie。之前没有3个错误。以下错误是:
注意:未定义的变量:第29行的C:\ xampp \ htdocs \ Assignment_3 \ login.php中的用户名 注意:未定义的变量:第34行的C:\ xampp \ htdocs \ Assignment_3 \ login.php中的用户名 警告:session_start():会话ID太长或包含非法字符,第37行的C:\ xampp \ htdocs \ Assignment_3 \ login.php中的有效字符为az,AZ,0-9和' - ,'
前两个是惊喜,因为我发誓我解决了这个问题。另一方面,最后一个错误对我来说是全新的。之前没有遇到过这个问题。我将发布我的login.php以获得更好的参考。再次,非常感谢任何帮助!
的login.php
<?php
//GOT USER ID COOKIE? IF SO GET IT
if(isset($_COOKIE["userid"])) {
$username = $_COOKIE["userid"];
}
//NO USER ID? GET THE COOKIE
else {
//SET COOKIE, EXPIRE IN 600 SECONDS
setcookie("userid", $username, time()+600 );
}
//SESSION ID IS OUR UNIQUE COOKIE
session_id($username);
//STARTING SESSION
session_start();
$_SESSION['logged_in'] = true;
//TOTAL AMOUNT OF TIME USER WAS LOGGED IN
$_SESSION['time_logged_in'] = time();
//THIS PAGE WILL PROMPT THE USER TO LOGIN WITH AN ALREADY REGISTERED USERNAME AND PASSWORD IN THE DATABASE
//IT WILL CHECK IN THE DATABASE TO SEE IF THE USERNAME AND PASSWORD ARE IN THE SAME ARRAY
//IF NOT THEN IT WILL SAY LOGIN FAILE.
//INCLUDE FUNCTIONS
include "functions.inc";
//IF THE ERRORS ARE SET TO FALSE, ECHO THE ERROR MESSAGES
if (!isset($errors)) $errors = array();
//INITIATE VARIABLE TO ACCESS THE USER DATABASE
$userfile = "user_data.dat";
$currentuser = "curren_user.dat";
//IF THE LOGIN BUTTON WAS PRESSED THEN VALIDATE THAT THE USER AND PASSWORD ARE IN THE SAME ARRAY
//IF NOT, THEN ECHO ERROR MESSAGE.
//IF THEY MATCH, THEN GO TO THE SUCCESSFUL LOGIN PAGE.
if (array_key_exists("login_btn", $_POST))
{
$logins = arrayfile_to_array($userfile);
$userlogin = $_POST['user'];
if (validate_login(strtolower($userlogin['name']), $userlogin['pass'], $logins))
{
$errors['login'] = "Invalid Login";
//break; taken out 11/9/2013
}
if (count($errors) == null)
{
//QUESTIONABLE VARIABLE
$_SESSION['username'] = $userlogin['name'];
header("Location: success_login.php");
}
}
//IF THE USER HITS THE REGISTER NEW USER BUTTON,
//THEN TAKES THEM TO THE REGISTER NEW USER PAGE.
if (array_key_exists("register_btn", $_POST))
{
header("Location: registration_page.php");
}
//PRINTING THE LOGIN USER INPUT FORMS.
//INCLUDES USERNAME AND PASSWORD.
//IF USERNAME AND PASSWORD DO NOT MATCH,
//HAS STUCKY FORM AND WILL ECHO ERROR MESSAGE AT TOP.
?>
<center>
<H1>Log In</H1>
<?php if (isset($errors['login'])) echo " <font color='red'>{$errors['login']}</font>"; ?>
<form method="POST" action="login.php">
<table>
<!--Text input for login-->
<tr><td>Username:</td>
<td><input type = 'TEXT'
name = 'user[name]'
value = '<?php if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["name"]; } ?>'></td></tr>
<br>
<tr><td>Password:</td>
<!--Password input for new password-->
<td><input type = 'PASSWORD'
name = 'user[pass]'
value = '<?php if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["pass"]; } ?>'></td></tr>
<br>
<tr></tr>
<br>
<!--Button to go back to form to fill again after errors are realized-->
<tr><td><input type = 'SUBMIT'
name = 'login_btn'
value = 'Login'></td>
<br>
<!--Button to go back to form to fill again after errors are realized-->
<td><input type = 'SUBMIT'
name = 'register_btn'
value = 'Register New User'></td></tr>
</table>
</form>
</center>
<?php
//CLEARS THE ERRORS ARRAY
$errors = array();
?>
答案 0 :(得分:0)
在该行:
else {
//SET COOKIE, EXPIRE IN 600 SECONDS
setcookie("userid", $username, time()+600 );
您正尝试在userid中指定名为$username
的var。
但我没有在任何地方看到它。
如果未定义,那么问题出在哪里。