0
當我在頁面上提交表單並導致會話結束並且用戶必須重新登錄時,我遇到問題。我花了數小時嘗試對此進行排序並且沒有隨處獲得它! 這是從哪兒形式是頁面的代碼 -當表單提交時結束的PHP會話
<?php
session_start(); /// initialize session
$title = 'Month Edit';
include("passwords.php");
$user_email = check_logged(1, $dbc);
?>
形式的代碼只是
的passwords.php頁面
<?php
require ('ct_db.php');//This is the database info
$query = "SELECT * FROM users";
$result = mysqli_query ($dbc, $query);
while($check_row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$this_us = $check_row['user'];
$this_ps = $check_row['pass'];
$USERS[$this_us] = $this_ps;
}
function check_logged($page, $dbc)
{
global $_SESSION, $USERS;
if (!array_key_exists($_SESSION["logged"],$USERS))
{
header("Location: login.php");
}
else
{
$this_user = $_SESSION["logged"];
$query = "SELECT * FROM users WHERE user = '$this_user'";
$result = mysqli_query ($dbc, $query);
$row2 = mysqli_fetch_array($result, MYSQLI_ASSOC);
$user_email = $row2['email'];
$user_permission = $row2['permissions'];
if($page == 3 && ($user_permission=="admin" || $user_permission=="user"))
{
header("Location: login.php");
}
else
{
if($page == 2 && $user_permission=="user")
{
header("Location: login.php");
}
else
{
echo '<div id="logged_in">';
echo '<form action="login.php" method="post"><input type="hidden" name="ax" value="out"> ';
echo 'Logged in as <span style="color:blue">'.$_SESSION["logged"]."</span> ";
echo 'email '.$user_email.' ';
echo '<input type="submit" value="Logout" id="log_out"/></div><br>';
return $user_email;
}
}
}
};
?>
我也有一個登錄頁面,其中包括以下
<?php
session_start();
$title = 'login';
require ('ct_db.php');
$query = "SELECT * FROM users";
$result = @mysqli_query ($dbc, $query);
while($check_row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$this_us = $check_row['user'];
$this_ps = $check_row['pass'];
$USERS[$this_us] = $this_ps;
}
function check_logged($page)
{
global $_SESSION, $USERS;
if (!array_key_exists($_SESSION["logged"],$USERS))
{
header("Location: login.php");
}
else
{
$this_user = $_SESSION["logged"];
require ('ct_db.php');
$query = "SELECT * FROM users WHERE user = '$this_user'";
$result = mysqli_query ($dbc, $query);
$row2 = mysqli_fetch_array($result, MYSQLI_ASSOC);
$user_email = $row2['email'];
$user_permission = $row2['permissions'];
if($page == 3 && ($user_permission=="admin" || $user_permission=="user"))
{
header("Location: login.php");
}
else
{
if($page == 2 && $user_permission=="user")
{
header("Location: login.php");
}
else
{
echo '<div id="logged_in">';
echo '<form action="login.php" method="post"><input type="hidden" name="ax" value="out"> ';
echo 'Logged in as <span style="color:blue">'.$_SESSION["logged"]."</span> ";
echo 'email '.$user_email.' ';
echo '<input type="submit" value="Logout" id="log_out"/>';
return $user_email;
}
}
}
};
require ('ct_db.php');
if ($_POST["ac"]=="log")
{ /// do after login form is submitted
if ($USERS[$_POST["username"]]==$_POST["password"]) /// check if submitted username and password exist in $USERS array
{
$_SESSION["logged"]=$_POST["username"];
$_SESSION["email"] = 'email address';
echo 'email = '.$_SESSION["email"];
}
else
{
echo 'Incorrect username/password. Please, try again.';
};
};
if ($_POST["ax"]=="out")
{ /// do after login form is submitted
if ($USERS[$_POST["username"]]==$_POST["password"]) /// check if submitted username and password exist in $USERS array
{
session_destroy();
echo 'You are logged out';
//header("Location: login.php");
die;
}
};
if (array_key_exists($_SESSION["logged"],$USERS)) //// check if user is logged or not
{
echo "You are logged in.";
echo '<form action="login.php" method="post"><input type="hidden" name="ax" value="out"> ';
echo '<input type="submit" value="Logout" />';
echo '</form>';
header("Location: mainPage3.php");
//// if user is logged show a message
}
else
{ //// if not logged show login form
echo '<div id="login">';
echo '<h1>Connectivity</h1>';
echo 'Please login<br><br>';
echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> ';
echo 'Username: <input type="text" name="username" id="username"/><br>';
echo 'Password: <input type="password" name="password" id="password"/><br>';
echo '<input type="submit" value="Login" />';
echo '</form>';
echo '</div><!--End login-->';
};
?>
在我試圖解決問題,我已經改變並添加了很多代碼,我相信它比應該更復雜。我可以在頁面之間移動頁面而不會被註銷,並且可以刷新包含任何問題的表單的頁面。但如果我按提交,我會回到登錄頁面。 我唯一能想到的可能是線索是用戶信息與網站信息位於不同的數據庫中,但我已檢查以確保每個人都擁有正確的代碼。 任何幫助感激地收到。
你不需要象'$ _SESSION'這樣的全球性聲明。 – Barmar
爲什麼您將所有用戶名和相關密碼存儲在數組中?只需查詢用戶名和密碼即可驗證是否存在。 –