doxn43207 2015-01-02 15:01
浏览 34

允许带有$ _Session的内容

I've been following some tutorials and managed to get my login and logout scripts working. What I"m now trying to do it get it to only allow access to pages when the user is logged in. Right now it's just redirecting users to the login page every time, which tells me that the session isn't being set or or my code is just wrong (and I've tried everything I can think of)

This is the login.php script that my form runs in order to set the session:

<?php
// establishing the MySQLi connection
require 'init.php';
if (mysqli_connect_errno())
{
    echo "MySQLi Connection was not established: " . mysqli_connect_error();
}

// checking the user
if(isset($_POST['login'])) {
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $pass = mysqli_real_escape_string($conn,$_POST['password']);
    $sel_user = "select * from login where username='$username' AND password='$pass'";
    $run_user = mysqli_query($conn, $sel_user);
    $check_user = mysqli_num_rows($run_user);
    if($check_user>0) {
        $_SESSION['username']=$username;
        echo "<script>window.open('index.php','_self')</script>";
    } else {
        echo "<script>alert('Sorry.  Your username or password is not correct, try again!')</script>";
    }
}
?>

And this is what I'm including at the top of every page:

<?php 
session_start();
if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) {
    header ("Location: account-login.php");
}

require 'init.php';
?>

I switched the login.php file from directing to a page to a popup telling me that I logged in and I get the popup, so the user and password are registering fine, it's just not storing the session somehow. Any ideas? Thanks!

  • 写回答

1条回答 默认 最新

  • douchao1864 2015-01-02 16:49
    关注

    OK, so I got it to work finally!

    Apart from all the comments (which helped a TON), I also decided to change the name I was setting in $_SESSION. I think it may be because the session name matched the name or POST data and that eas causing a conflict somewhere.

    Changed this:

    $_SESSION['username']=$username;
    

    Which I think conflicted to this:

    $_SESSION['session_id']=$username;
    

    Worked!

    THANK YOU!!!!!!!

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度