我在使用外部登录文件时遇到问题。基本上我想去一个外部登录 php 页面并验证表单。验证表单后,我想重定向回上一页并开始会话。我遇到的问题是我的会话没有启动(即它不会将登录表单更改为注销按钮)。我的代码在下面。提前致谢。
索引.php
<?
session_start();
include('phpFunctions/databaseConnect.php');
include('phpFunctions/reusableFunctions.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Unnamed classifieds site</title>
<script type="text/javascript" src="js/formValidation.js"></script>
</head>
<body>
<div class="wrapper">
<div class="login">
<? if(isset($_GET['error'])){$error = $_GET['error'];} ?>
<span id="error_message" class="error"><? if(isset($error)){echo $error;} ?><span id="login_error"></span></span>
<? login(); ?>
</div>
<div class="mainLogo">
<? mainLogo(); ?>
</div>
<div class="search_form">
<span id="error_message" class="error"><span id="search_error"></span></span>
<? search(); ?>
</div>
<div class="footer">
<? footer(); ?>
</div>
</div>
</body>
</html>
登录.php
<?
include('phpFunctions/databaseConnect.php');
$error = array();
$email = $_POST['email'];
$password = $_POST['password'];
if(empty($email) || empty($password)){
$error[] = 'Please fill in the form!';
}else if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/',$email)){
$error[] = 'Your email is not in the correct format!';
}else if(strlen($password) < 6){
$error[] = 'Your password must be at least 6 characters long!';
}else{
$hashPass = md5($password);
$query_user = mysql_query("SELECT * FROM users WHERE email='$email' AND password='$hashPass'") or die(mysql_error());
$num_rows = mysql_num_rows($query_user);
if($num_rows == 1){
while($row = mysql_fetch_array($query_user)){
$_SESSION['id'] = $row['id'];
header('Location:index.php');
}
}else{
$error[] = 'Sorry no user with that email or password!';
}
}
if(!empty($error)){
foreach($error as $key => $values){
$error_message.= "$values";
}
header('Location: index.php?error='. urlencode($error_message));
}
?>