dongwen9051 2011-08-29 03:13
浏览 36
已采纳

密码保护页面?(使用db访问)

Couple questions here: My end goal is to password protect the file logged_in.php.

Note: I'm only a beginner/intermediate programmer so i would like clear explanations, please.

First off, i have set a username and password within a database table.

  1. I have two pages: login.php and logged_in.php(names are just for example purposes). How do i "require" a user to first go through login.php(the log in process) in order to gain access to logged_in.php(if the entered username/password are correct)?

  2. Is this the best way to password protect a page?

What i've tried:

Login.php:

<?php
            $db_host="host";
            $db_user="user";
            $db_pass="pass";
            $db_name="name";
            $db_table="table";
            $user = mysql_real_escape_string(strip_tags($_POST['user']));
            $pass = mysql_real_escape_string(strip_tags($_POST['pass']));

            mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());

            if(isset($user) && isset($pass))
            {
                $sql = "SELECT * FROM $db_table WHERE username='$user' AND password='$pass'";
                $result = mysql_query($sql);
                $count = mysql_num_rows($result);
                if($count == 1)
                {
                    header("location:logged_in.php");
                }
                else
                    header("location:bad_login.html");
            }
        ?>
  • The problem with my code at the moment is that, someone can directly type in the URL of logged_in.php and access the page without being "required" to go through login.php first(i'm sure this is obvious to everyone..).

  • I put require(login.php); at the top of logged_in.php; however, that didn't work out.

  • I've checked google for some good tutorials on this topic, unfortunately i couldn't find any that had clear explanations.

  • I also saw a few other questions regarding this topic on stackoverflow, but they didn't really help me out.

I'm also interested in being able to pass-protect my page using the method phpMyAdmin uses(when you type in the URL and press enter it drops down a menu from the top of the browser asking for a username/password). I don't know how that works. If someone can tell me how that works i'm willing to completely disregard the method i'm attempting to use at the moment(if the phpMyAdmin method is secure enough and is fairly easy to implement).

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douri4459 2011-08-29 03:22
    关注

    Use $_SESSION variable:

    <?php
    
                session_start();
    
                $db_host="host";
                $db_user="user";
                $db_pass="pass";
                $db_name="name";
                $db_table="table";
    
                mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
                mysql_select_db($db_name) or die(mysql_error());
    
                $user = mysql_real_escape_string(strip_tags($_POST['user']));
                $pass = mysql_real_escape_string(strip_tags($_POST['pass']));
    
                if(isset($user) && isset($pass))
                {
                    $sql = "SELECT * FROM $db_table WHERE username='$user' AND password='$pass'";
                    $result = mysql_query($sql);
                    $count = mysql_num_rows($result);
                    if($count == 1)
                    {
                        $_SESSION['username'] = $user;
                        header("location:logged_in.php");
                        exit();
                    }
                    else
                        header("location:bad_login.html");
                        exit();
                }
            ?>
    

    logged_in.php:

    <?php
    
    session_start();
    
    // check if $_SESSION was setting before
    if (!isset($_SESSION['username']))
    {
        header("Location: login.php?e=access_denied");
        exit();
    }
    ?>
    

    The phpMyAdmin login is different because use the MySQL username and password to login, so phpMyAdmin does not need to create a database and table to login like your code

    Also you need the logout:

    logout.php

    <?php
    
    session_start(); // <-- Oops!!
    
    // unset all $_SESSION variables
    session_unset();
    session_destroy();
    header("Location: logged_in.php?m=logout_success");
    exit;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算