dongzhang7961 2017-12-12 14:05 采纳率: 0%
浏览 99
已采纳

如何在登录时才能访问页面

How to make an page only accessible when logged in, in PHP?

I want you when you click on the background to defend two buttons with Login And Register and you can only access these two buttons after you log in.

<?php
session_start();
include_once 'dbconnect.php';

?>
<!DOCTYPE html>

<HTML>
<head>
    <title>NCS pagina principala</title>
    <meta content="width=device-width, initial-scale=1.0" name="viewport" >
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" >

</head>
<body>

                <?php if (isset($_SESSION['usr_id'])) { ?>
                <FONT size="4%" color="#66ffff" FACE="cursive">Esti conectat cu:</FONT><i><FONT size="5" color="#66ff66"> <?php echo $_SESSION['usr_name']; ?></FONT></i>

                <link rel="stylesheet" href="styles/buttonstyle.css">
                                 <a class="button" href="logout.php">Delogheaza-te</a>

                <?php } else { ?>
 <link rel="stylesheet" href="styles/buttonstyle.css">
<a class="button" href="login.php">Logheaza-te</a>
<a class="button" href="form.html">Cerere cont</a>
                <?php } ?>

<head>

<a class="button" href="blacklist.php">BlackList</a>
<CENTER>


<BODY STYLE = "BACKGROUND: url(https://cdn.discordapp.com/attachments/389773843813629972/389781868247253002/thumb-1920-553248.jpg); BACKGROUND-SIZE:130%"></BODY>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

</HTML>
  • 写回答

1条回答 默认 最新

  • dongpu5874 2017-12-12 14:11
    关注

    try this (after session_start(); ):
    if (!isset($_SESSION['usr_id'])) header("Location:logout.php");


    and logout.php must be

    <?php 
    session_start();
    session_destroy();
    header("Location:login.php");
    ?>
    

    then, in login.php you write your login code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi