如果用户尚未登录,如何将其重定向到登录页面? [英] How to redirect users to login page if they haven't logged in?

查看:342
本文介绍了如果用户尚未登录,如何将其重定向到登录页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录页面login.php具有以下代码:

I have the following code for my login page login.php:

<form method="post" action="confirmLoginCredentials.php">
<h2>LOGIN</h2>
    <p>Username: <input type="text" name="username" /></p>
    <p>Password: <input type="password" name="password" /></p>
    <p><input type="submit" name="submit" value="Login" /></p>

</form>

提交后,它将重定向到confirmLoginCredentials.php,即:

After submitting, It redirects to confirmLoginCredentials.php which is:

 <?php

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']);

require_once 'config.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$q = "SELECT first_name, last_name FROM users WHERE user_name = '$username' AND password = '$password'";

$result = $mysqli->query($q) or die(mysqli_error($mysqli));


if (!mysqli_num_rows($result) == 1) {
    header("Location: login.php");  
    }
else {
    setcookie('authorized', 1, 0);
    header("Location: index.php");
}

?>

这可以正常工作,并且如果用户已成功登录,它将用户重定向到索引页面.如果我的网站上所有页面尚未登录,如何将用户重定向到login.php页面? (换句话说,如果用户尚未登录,则用户将无法访问我的网站的内容)我应该在我网站的所有其他页面中输入什么代码?

This works fine and it redirects the user to the index page if they have logged in successfully. How do I redirect the user to the login.php page from all pages in my website if they have not yet logged in? (in other words, the user cannot access the contents of my site if they have not logged in) what code should i put in all other pages of my site to do this?

任何帮助将不胜感激!! 谢谢!!

any help will be very much appreciated!! thanks!!

推荐答案

注意:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆