[英]Checking if user is logged in
我已按照本教程進行操作,以便為內容管理站點構建登錄系統。 http://www.formget.com/login-form-in-php/ 。 我希望以某種方式設置站點,以便用戶需要登錄才能查看該站點,並且如果用戶通過URL輸入站點名稱,則他們將立即定向到登錄頁面。
當前,當我直接進入頁面時,出現錯誤提示:
注意:未定義的索引:第6行的C:\\ xampp \\ htdocs \\ WebDevelopment \\ V18 \\ CMS \\ session.php中的login_user
使用的文件:
login.php
<?php
session_start();
$error = ''; // variable to store error message
if (isset($_POST['login'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else {
// Define username and password
$username = $_POST['username'];
$password = $_POST['password'];
$connection = mysql_connect("localhost", "root", "");
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// select DB
$db = mysql_select_db("v18_apartments", $connection);
$query = mysql_query("Select * from login where password = '$password' AND username = '$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // initializing session
header("location: CMS-home.php");
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection);
}
}
?>
index.php
<?php
include ('login.php');
if (isset($_SESSION['login_user'])){
header("location: CMS-home.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> V18 - Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
</head>
<body>
<div id="wrapper">
<div class="center">
<div id="login-form">
<form action="" method="POST">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="********">
<button type="submit" name="login">Submit</button>
<h2><?php echo $error; ?></h2>
</form>
</div>
</div>
</div>
</body>
</html>
session.php
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("v18_apartments", $connection);
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("select username from login where username = '$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session = $row['username'];
if (!isset($login_session)) {
mysql_close($connection);
header('Location : index.php');
}
?>
logout.php
<?php
session_start();
if (session_destroy()) // destroy all sessions
{
header("Location: index.php");
}
?>
這個怎么樣?
session.php
<?php
session_start();
if(!isset($_SESSION['login_user']))
header('Location : index.php');
exit;
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.