dongxuxian1123 2015-03-15 08:08
浏览 27
已采纳

我如何写限制访问页面?

I wrote custom login script for users to login to the main control page. I found out that even when users are not login they can still visit the main control of which I want someone to help me to write a restrict access to page().

Please look through my php login script and based on that code help me write the restrict access to the main control page. assume that my main crontrol page is: cecontrolpage.php

I know we use $_SESSION to that but I have little idea of it.

this is my login.php code which is working fine:

<?php
Session_start();
$Email       = $_POST["email"];
$Password    = $_POST["password"];
$cn          = "localhost";
$db_username = "root";
$pas         = "***";
$db_name     = "cemembers";
//Open a connection to a MySQL Server
if ($Email && $Password) {
    $connect = mysqli_connect($cn, $db_username, $pas, $db_name) or die("Could not connect to database");
    //sending MySqli query
    $query   = mysqli_query($connect, "SELECT * FROM users WHERE Email= '$Email'");
    $numrows = mysqli_num_rows($query);
    //After PHP declaration we are going to create index file[form]
    if ($numrows !== 0) {
        while ($row = mysqli_fetch_array($query)) {
            $dbEmail    = $row["Email"];
            $dbPassword = $row["Password"];
        }
        if ($Email == $dbEmail && $Password == $dbPassword) {
            header("location:ce membership birthday system control_pannel.php");
            @$_SESSION("Email") == $Email;
        } else
            header("location:index.php?login_attempt=1");
    } else
        header("location:index.php?login_attempt=2");
} else
    header("Location:index.php?login_attempt=0");
?>

please can someone help me write the php code to restrict access to cecontrol.php ??

Please STEP by STEP with php comments on each part.

  • 写回答

2条回答 默认 最新

  • drby30217 2015-03-15 08:16
    关注

    First you have to save user values in session after authentication from database like
    $_SESSION['username'] = "name"; $_SESSION['user_id'] = 1;

    function check_session() {
                                session_start();
                                if ($_SESSION['user_id']=='')
                                {
                                   // redirect to login 
                                }
                            }
    

    On every page that you want to restrict access you can call check_session().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作