我试图让用户登录,直到他们点击了注销链接或关闭了浏览器。
我遇到的问题是,对于teacherlogin.php页面,它只显示一个空白页面。没有php错误或javascript错误所以我的问题是我在下面的teacherlogin.php代码中做错了什么;
teacherlogin.php
<?php
/*
file: login.php
authorized (logged in) users must be redirected to a secure page (member.php) or (secure.php)
unauthorized (guests) users have to see the login form
*/
#include the class file and start it.
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
require_once('session.class.php');
$session = new Session();
#check user's access
if($session->get("auth"))header("location:members.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Teacher Login</title>
<link rel="stylesheet" type="text/css" href="TeacherLoginStyle.css">
</head>
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
// required variables (make them explciit no need for foreach loop)
if (isset($_POST['submit'])) {
$teacherusername = (isset($_POST['teacherusername'])) ? $_POST['teacherusername'] : '';
$teacherpassword = md5(md5("g3f".$_POST['teacherpassword']."rt4"));
// don't use $mysqli->prepare here
$query = "SELECT TeacherId, TeacherForename, TeacherSurname, TeacherUsername, TeacherPassword, Active FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$teacherusername,$teacherpassword);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherId, $dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword, $dbActive);
while($stmt->fetch()) {
if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
if ($dbActive == 0) {
$error = "You Must Activate Your Account from Email to Login";
}else{
$session->set('auth',TRUE);
$session->set('active',TRUE);
$session->set('teacherid',$dbTeacherId);
$session->set('teacherusername',$dbTeacherUsername);
header('Location: members.php') ;
}
}else{
//password and username dont match
$error = "The Username or Password that you Entered is not Valid. Try Entering it Again";
}
}
/* close statement */
$stmt->close();
/* close connection */
$mysqli->close();
}
include('noscript.php');
?>
<body>
<h1>TEACHER LOGIN</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" id="teachLoginForm">
<p>Username</p><p><input type="text" name="teacherusername" /></p> <!-- Enter Teacher Username-->
<p>Password</p><p><input type="password" name="teacherpassword" /></p> <!-- Enter Teacher Password-->
<p><input id="loginSubmit" type="submit" value="Login" name="submit" /></p>
</form>
<a href="./forgotpass.php">Forgot Password</a>
</body>
</html>
session.class.php
<?php
/*
Simple PHP Session Class:
a simple class to help managing the Session function in
php for beginners and it introduces them to OOP where
they can modify it and add new features and extend its
functionality (session in database, track users actions,etc)
*/
#Session settings.
#Session cookie lifetime at the user browser. (seconds)
ini_set('session.cookie_lifetime', '0');
#Read the functions reference below before you change these values.
ini_set('session.gc_maxlifetime',7200);# 2 hours.
ini_set('session.gc_probability', '1'); #default PHP value.
ini_set('session.gc_divisor', '100'); #default PHP value.
/*====================================================================
ini_set('session.cookie_lifetime', '0');
User cookie life time in seconds.
0 means the cookie wont expire until the user closes the broswer
ini_set('session.gc_maxlifetime',7200); #default: 1440 (24 mins).
Session Garbage Collection cleaner (GC).
7200(seconds) equals to (2 hours): The GC will try to
clean session data in the server for (users who logged
out, closed the browser AND users who are inactive for
more than that time) however the clean function does
not run directly, read the next block for more explaination.
PHP manual:
session.gc_maxlifetime specifies the number of
seconds after which data will be seen as 'garbage'
and potentially cleaned up. Garbage collection may
occur during session start
(depending on session.gc_probability and session.gc_divisor).
ini_set('session.gc_probability', '1'); #default PHP value (1).
ini_set('session.gc_divisor', '100'); #default PHP value (100).
Garbage Collection (GC) Settings:
PHP manual:
session.gc_divisor coupled with
session.gc_probability defines the probability
that the gc (garbage collection) process is started
on every session initialization. The probability is
calculated by using gc_probability/gc_divisor
e.g. 1/100 means there is a 1% chance that the GC
process starts on each request.
session.gc_divisor defaults to 100.
====================================================================*/
/*
HOW TO USE:
- include the session file in all your files and call the session object
require_once('session.class.php');
$session = new Session();
GET SESSION ID:
- $session->sid;
@return type (string)
@example:
echo $session->sid;
GENEREATE NEW SESSION ID:
- $session->re();
@return type (string)
#note: returns the value of the new session id.
@example:
$sid = $session->sid;
$new_sid = $session->re();
echo "My Session ID is: {$sid} but I got a new one now: {$new_sid};
ASSIGN NEW SESSION VALUE:
- $session->set($key,$value);
@return type (void)
@examples:
$session->set("name","foo bar");
$session->set("age",24);
$session->set("auth",TRUE);
GET SESSION VALUE:
- $session->get($key);
@return type (boolean, int, string, array).
#note: returns FALSE if the value is not set
@examples:
echo $session->get("name");
if($session->get("auth")) echo "hello authorized user!";
if(($age = $session->get("age")) > 21)
echo "your age is {$age}, you can drive!";
DELETE SESSION VALUE:
- $session->delete($key);
@return type (void);
@example
$session->delete("age");
DESTROY SESSION
- $session->destroy();
@return type (void);
*/
/*==================================================================*/
class Session{
public $sid;
public function __construct() {
@session_start();
$this->sid = session_id();
}
public function re(){
@session_regenerate_id();
$this->sid = session_id();
return $this->sid;
}
public function set($key, $val) {
$_SESSION[$key] = $val;
return true;
}
public function get($key) {
if ( isset($_SESSION[$key]) ) {
return $_SESSION[$key];
}
return false;
}
public function delete($key) {
unset($_SESSION[$key]);
}
public function destroy() {
$_SESSION = array();
session_destroy();
}
}
?>
member.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
/*
file: secure.php, profile.php, member.php
authorized (logged in) users => log them out and show goodbye msg or send them to login.php
unauthorized (guests) users => redirect them to login.php
*/
require_once('session.class.php');
$session = new Session();
#if user is not logged in, he will be sent to the login.php page
#note the (!) sign before the $session, it means if the $session->get("auth") == false
if(!$session->get("auth")) header("location:teacherlogin.php");
echo "hello, you are logged in";
echo "<br />";
echo "username: ".$session->get("teacherusername");
echo "<br />";
echo "teacherid: ".$session->get("teacherid");
echo "<br />";
echo "active status: ".$session->get("active") ? "Active" : "Not Active";
echo "<br />";
echo "<a href='logout.php'>Log out</a>";
?>
答案 0 :(得分:0)
把它放在脚本的最顶层......
error_reporting(E_ALL);
ini_set('display_errors','1');
这应该强制在屏幕上显示错误。如果没有,请检查您的Web服务器日志,以获取有关此页面加载时服务器响应的详细信息。你将能够在那里看到错误信息。