doudeng2057 2015-09-07 23:51
浏览 53

PHP取消设置会话和cookie

I'm new at programming and I'm working with some PHP. I was given a file called login.php and some directions to create a second file called admin.php.


Here are the instructions.

admin.php

  • If the user tries to access this file without logging in, re-direct them back to the login.php page which should display a message saying “Invalid Login” – use the session variable to check.

  • If they are logged in:

    • provide them with the value of the “loggedIn” cookie with the message: “You logged in January 25, 10:00am” (or whatever the value is)

    • unset the session variable and destroy the session and

    • unset both cookies (session and ‘loggedIn’) and set to expire so it will be removed by the browser.

    • NOTE: test the already logged in portion of login.php before adding the code to destroy the session and the cookie.

Once you’ve got the above working, change your code so that if they are redirected to login.php from admin.php, it should display a different message: “You need to log in”.

<?php
    session_start();
    $message = null;

    if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == 'true') {
        header("Location: admin.php");
        exit();
    }

    if ( $_SERVER['REQUEST_METHOD'] == 'GET') {
        if ( sizeof($_GET) && isset($_GET['username']) && isset($_GET['password']) && $_GET['username'] && $_GET['password']) {
            if ($_GET['username'] == 'username' && $_GET['password'] == 'password') {
                $_SESSION['loggedIn'] = 'true';
                setcookie ("loggedIn", date("F d,Y h:ia"),  time()+60*10, "/", $_SERVER['SERVER_NAME']);
                header("Location: admin.php");
                exit();
            } else {
                $message = 'Invalid Login';
            }
        } else {
            $message = 'Invalid Login';
        }
    }

?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <?php if($message) echo '<div class="warning">' . $message .       '</div>'; ?>
        <form method="get">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" value="username" />
            <label for="password">Password</label>
            <input type="password" name="password" id="password"  value="password" />
            <input type="submit" name="submit" value="Login" />
        </form>
    </body>
</html>

The code below is all I have so far. I want to know if my unsetting is correct with the session variables and cookies. Also, how do I display the date and time as stated in the instructions, and how do I display a message of "You need to log in if the user is directed to the login.php page from the admin.php page?

<?php
session_start();

if (!isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == 'false') {
        header("Location: login.php");
        exit();
    } else {
        unset($_SESSION['loggedIn']);
        session_unset();
        session_destroy();
        unset($_COOKIE['loggedIn']);
    }
?>
  • 写回答

3条回答 默认 最新

  • dppn67180 2015-09-08 00:01
    关注

    I think I under stand what you are asking for completely. Let me know if it's not right.

    First for message, I would do it by having it redirect to a GET URL so maybe http://example.com/login.php?login=error or something like that.

    So then you would say

    if($_GET['login'] == 'error') {
        echo '<h1>You Need to login</h1>';
    }
    

    Now for the date and time I would look at w3 school so you can find the formatting you would like. If you still can't figure it out let me know.

    And lastly for unsetting cookies, you should set the cookie equal to nothing. So setcookie("username", "")

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c