0

这个登录脚本有什么问题?我有一个模态样式的 iframe 登录框,/login.php我有一个/index.php.

登录后,login.php 重定向到 index.php,但我没有看到用户名。只是Hello Guest。当我尝试登录时,它说you, username, are already logged in

登录.php:

<?php session_start(); require_once('connections/Main.php');

if(isset($_SESSION['username'])) {

    echo '<div class="error_message">Attention! You, '.$_SESSION['username'].' are already logged in.</div>';
    echo "<br />";
    echo "Go <a href='javascript:history.go(-1)'>back</a> to the page you were viewing before this.</li>";

    exit();
}
?>

索引.php:

<?php session_start(); 
   require_once('connections/Main.php'); 
     ?>

<div> Hello <?php   if(isset($_SESSION['username'])) { 
          echo $_SESSION['username']; }
                    else{ echo 'Guest'; } ?></div>
4

2 回答 2

0

你打session_start()了两次电话,这是不必要的。logged_in登录后,设置会话变量true。然后使用

if($_SESSION['logged_in']){
 //Do what you want for the logged in user
}

确实,您可以在一个 login.php 中完成所有这些工作。有这样的事情:

//if reached from login form
if ($_POST['email']){
  //validate the user
  // if validates, log him in by:
    $_SESSION['logged_in'] = true;
    $_SESSION['email']= $_POST['email'];
  //do other fancy database save stuff
 }
//else show him login form
else{
  <form>
   //<input />.. etc
  </form>
}
于 2012-07-02T00:36:57.180 回答
-2

以此开始您的会话。

$_SESSION["im_going_in"]=true; 
$_SESSION["username"]=$username;//get data from db.

索引.php

<?php
if($_SESSION["im_going_in"]) {
?>

  <?=strtoupper($_SESSION["username"])?>// welcome dear bla bla...

<?
}
?>
于 2012-07-01T21:42:49.537 回答