通过键入 URL 防止直接访问网页 [英] Prevent direct access to a webpage by typing the URL

查看:42
本文介绍了通过键入 URL 防止直接访问网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 goDaddy 上托管我的网站(我们称之为 abc.com).

So I'm hosting my website (let's call is abc.com) on goDaddy.

我有一个登录页面 (abc.com/login.html).

I have a login page (abc.com/login.html).

一旦登录凭据得到验证,我就会进入第二个名为预订的页面 (abc.com/booking.html).

Which takes me to a second page called booking (abc.com/booking.html) once the login credentials are verified.

所以我不希望人们只能输入 abc.com/booking.html 并访问它.我希望他们访问 abc.com/login.html,然后访问 abc.com/booking.html

So I don't want people to be able to just type abc.com/booking.html and access it. I want them to go to abc.com/login.html and then go to abc.com/booking.html

所以我遇到了两种方法来解决这个问题 -

So I came across 2 ways to fix this -

  1. 在booking.html 中包含一个验证php 脚本并将扩展名从html 更改为phtml.-> 这对我不起作用
  2. 包含一个 .htacess 文件.-> 我真的不知道该怎么做

推荐答案

所以你的登录屏幕应该已经实现了会话代码,它有一个变量来指定用户是否登录.如果您还没有实现,代码看起来类似于:

so your login screen should already have session code implemented into it that has a variable that specifies if the user is logged in or not. If you don't have that implemented yet, the code would look similar to:

<?php session_start();//at the very top of the page
?>
//... your own code
//if the user successfully logs in then:
$_SESSION['authenticated']=true;

然后在booking.php页面上(它应该是允许php脚本的php,这对于验证用户是否登录非常重要),然后您将检查用户是否登录.如果他登录了,其余的页面加载,如果他没有,你会将它们重定向到 login.php:

Then on the booking.php page (it should be php to allow php scripts which is super important for validating if a user is logged in), you would then check if the user did log in. If he did, the rest of the page loads, if he didn't, you would redirect them to login.php:

在booking.php 的最顶部:

at the very top of booking.php:

<?php session_start();
if (!isset($_SESSION['authenticated']))
{
    //if the value was not set, you redirect the user to your login page
    header('Location https://www.example.com/login.php');
    exit;
}
else
{
   //if the user did login, then you load the page normally
}

这篇关于通过键入 URL 防止直接访问网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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