注意:首先我使用 PHP、HTML、CSS、MySQL(数据库),还使用 Dreamweaver 6 编辑我所有的代码。
好的,所以对于我的最后一年项目,我正在创建一个电子考勤跟踪和监控系统。我在为多个用户创建登录名时遇到了很大的困难。该系统的三个用户是工作人员、家长和学生。为此,我专注于员工
好的,我有...
- staff_register.php
- staff_verify.php
- staff_login.php
- staff_home.php
因此,当员工想要注册时,他们必须先注册。所以我有一个表单,在提交时可以完美地写入数据库。 student_register.php 发布到 student_verify.php,其中会向用户发送一封激活电子邮件,要求他们验证自己(单击链接)。一旦用户验证,他们就成为活跃用户。然后他们进入登录页面。当用户输入他们的详细信息时,我将被重定向回 index.php,在我的代码中,我特别指出要重定向到 staff_home.php 下面我将提供一些代码:
这是 staff_login.php:
<?php include_once("scripts/global.php");
$message = '';
` `if(isset($_POST['email'])){
$email = $_POST['email'];
$pass = $_POST['pass'];
$remember = $_POST['remember']; `
//error handeling
if( (!$email) || (!$pass) ){
$message = 'please insert both fields';
}else{
//secure the data
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
$query = mysql_query("SELECT * FROM staff WHERE email='$email'
AND password='$pass' LIMIT 1") or die("Could not check member");
$count_query = mysql_num_rows($query);
if($count_query == 0){
$message = 'The infomation you entered was incorrect';
}else{
//start the sessions
$_SESSION['pass'] = $pass;
while($row = mysql_fetch_array($query)){
$username = $row['username'];
$id = $row['id'];
}
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
if($remember == "yes"){
//create the cookies
setcookie("id_cookie", $id, time()+60*60*24*100, "/");
setcookie("pass_cookie", $pass, time()+60*60*24*100, "/");
}
header("Location: staff_home.php");
}
}
index.php 的 PHP 脚本是
<?php include_once("scripts/global.php");
if ($logged == 1) {
header("Location: staff_home.php");
exit;
}
?>
我将 staff_home.php 放在索引中,因为当我不断被重定向到 index.php 时,我将代码放在顶部,希望它只是将我重定向到 staff_home.php
staff_home.php 的 PHP 脚本是
<?php include_once("scripts/global.php");
if ($logged == 0) {
header("Location:staff_home.php");
exit();
}
?>
我怀疑我可能必须分配用户角色,但我还没有这样做。
所以我想要的是员工登录并重定向到 staff_home.php,学生登录并重定向到 student_home,家长登录并重定向到 parent_home。
我已经尝试练习 PHP 几个月了,但最微小的错误真的让我很烦恼,哈哈。我祈祷有人能帮助我!这可能看起来像是一个小错误,但我将永远感激任何关于如何改进我的代码并使其正常运行的提示和建议。我会回来查看是否有人回复,我的电子邮件是 jmudhar90@hotmail.co.uk。
问候人们
最佳答案
我在任何时候都没有在您的代码中看到 session_start()
。在页面中使用 session 之前,您需要调用 session_start()
。
关于php - 用户登录被重定向到 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15012515/