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

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条