1

我有一个简单的身份验证:您登录 login.php 页面,然后您被重定向到 home.php 页面。

这是 login.php 的代码:

if(pg_num_rows($rs) == 0){ //I search in db for a row with username and password
        $errMess = "error";
        pg_close($conn); 
    }else{
        $row = pg_fetch_row($rs);
        session_start(); 
        $_SESSION['username']=$_POST["nick"];
        $_SESSION['admin'] = $row[0];
        pg_close($conn); 
        header("Location: /home.php");
    }

现在在家里,我以这种方式完成了标题:

<?php require_once("scripts/functions.php"); 
      require_once("scripts/config.php");
      session_start(); 
?>
 <div id="siteHeader" class="headersLeft"><?php echo WELCOME;?></div>
        <div id="userContainer" class="headersRight"> 

            Logged as: <?php echo getDisplayName(); ?>
            <?php if(isset($_SESSION['username'])) {?>
                <button class="button" onclick="location.href='/logout.php';">logout</button>
            <?php }else{ ?>
                <button class="button" onclick="location.href='/login.php';">login</button>
            <?php }
            ?>
        </div>

它不起作用:即使数据正确,它仍然给我“客人”,会话变量在标题段落中丢失了..怎么来的?

4

4 回答 4

1

已解决:我在 windows 下,php 实际保存会话文件的 temp 文件夹的默认路径是错误的:是“/tmp”并且无法识别。我将它设置为“C:\php\tmp”并且它起作用了:会话文件根本没有保存!

于 2013-11-03T13:27:11.893 回答
0

I'm guessing there's some more code after the if statement that continues to manipulate $_SESSION. That's where $_SESSION['username'] is assigned the 'guest' value.

Remember, header("Location: /home.php"); only sets a response header. It doesn't redirect immediately, stopping script execution.

Place a exit; command right after header() to prevent execution from reaching the rest of the code:

    header("Location: /home.php");
    exit;
于 2013-11-02T21:01:18.820 回答
0

编写 session_start(); 在一切之上(紧接着

<?php 
session_start(); 
require_once("scripts/functions.php"); 
require_once("scripts/config.php");
?>

或者如果仍然不起作用,那么像这样编写代码:

<?php 
ob_start();
session_start(); 
require_once("scripts/functions.php"); 
require_once("scripts/config.php");
?>

另外不要忘记将这两行放在 login.php 页面的顶部。希望能帮助到你 :)

于 2013-11-02T19:50:54.430 回答
0

这对我有用:

session_save_path ( "" ) ;  
session_start();
于 2014-09-14T15:52:35.343 回答