我真的很难与整个'只有登录的用户可以查看此页面'。 Php对我来说是新的,我似乎无法弄清楚这一点。也许这是一个愚蠢的问题,或者我的代码不对,但我真的想弄清楚这一点。
的login.php:
<?php
session_start();
function is_logged() {
if (isset($_SESSION['username'])) return $_SESSION['username'];
else return false;
}
if (is_logged()) {
$user_id = is_logged();
do_something($user_id);
} else {
if (isset($_POST['submit'])) { //form submitted
//check login and password, if they are correct, do this:
$_SESSION['username'] = $username_from_database;
//if not correct
unset($_SESSION['username']);
header('Location: welcome.php'); //refresh page
} else {
//show login form with button named 'submit'
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<?php
} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * from GEBRUIKERS WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
echo "<p>Logged in successfully</p>";
// do stuffs
}
if (mysqli_num_rows($result) > 0) {
// Output data of each row
while($row = mysqli_fetch_assoc($result)) {
$_SESSION['+login_user']=$user; // Initializing Session
header("location: welcome.php"); // Redirecting To Other Page
}
}
else {
$error = "Username or Password is invalid";
}
mysqli_close($conn); // Closing Connection
}
?>
</body>
</html>
的welcome.php:
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css"/>
<!--Header wordt opgehaald-->
</head>
<?php
require "header2.php"
?>
<?php
$servername = "localhost";
$username = "";
$password = "";
$database = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
<body>
<?php
//Perform queries
$sql = "SELECT acteur_voornaam, acteur_tussenvoegsel, acteur_achternaam, acteur_geboortedatum FROM FILM_ACTEURS";
$result = $conn->query($sql);
//Films
if ($result->num_rows > 0) {
echo "<table style='border: solid 1px grey; margin-left: auto; margin-right: auto; margin-top:50px;'><th>Voornaam</th><th>Tussenvoegsel</th><th>Achternaam</th><th>Geboortedatum</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["acteur_voornaam"] . "<td>" . $row["acteur_tussenvoegsel"]. "<td> " . $row["acteur_achternaam"]. "<td> " . $row["acteur_geboortedatum"] . "" . "</td></tr>";
}
echo "<table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
<?php
//Footer wordt opgehaald
include "footer.php"
?>
</html>
答案 0 :(得分:0)
在session_start();并使用is_logged()进行检查;必须包含在所有仅限会员的页面中,还有另一个原因是您的代码无法正常工作,也就是说您在注销后没有清除会话变量,因此您的浏览器会自动登录
答案 1 :(得分:0)
在您的登录功能中,在成功登录块内创建一个会话变量,如:
$_SESSION['loggedIn'] = true;
现在,在每个页面上,需要登录才能访问该页面,请进行以下检查:
if( !isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] != true) )
{
// redirect the user to login screen if the session variable is not set and its value is not true
header('location: login.php');
}
注意:要访问会话,您必须在每个页面上放置session_start()
,并且它必须是第一行。
答案 2 :(得分:0)
首先创建一个名为session.php
的页面,您必须将其包含在所有页面中
<?php
session_start();
function is_logged() {
if (isset($_SESSION['username'])) return $_SESSION['username'];
else return false;
}
if (is_logged()) {
$user_id = is_logged();
do_something($user_id);
} else {
if (isset($_POST['submit'])) { //form submitted
//check login and password, if they are correct, do this:
$_SESSION['username'] = $username_from_database;
//if not correct
unset($_SESSION['username']);
header('Location: welcome.php'); //refresh page
} else {
//show login form with button named 'submit'
}
}
?>
第二:在所有页面中包含session.php
页面。这将检查会话或重定向到登录页面。
在欢迎页面中:页面顶部的Welcome.php
包含session.php
,如:
<?php
inlcude 'session.php';
?>
如果需要,请在session.php
文件中进行必要的更改。
注意:您可以为session.php
文件提供任何名称。
答案 3 :(得分:0)
在php中尝试使用isset函数。会话启动后的bellow代码放在welcome.php文件中。
if(!(isset($_SESSION['username']) && $_SESSION['username'] != '')){
header ("Location: login.php");
}else{
header ("Location: welcome.php");
}
如果未设置会话“用户名”,则在登录页面上进行此重定向。如果会话已初始化,则重定向到welcome.php。