2014-09-29 27 views
-4

好了,所以这是我的login.php页面的PHP代码文件:PHP和MySQL店用户ID登录完成课程后

<?php 
    if (isset($_GET['action'])) { 
     switch (strtolower($_GET['action'])) { 
      case 'login': 
       if (isset($_POST['username']) && isset($_POST['password'])) { 
        if (!validateUser($_POST['username'], $_POST['password'])) { 
         $_SESSION['error'] = "Bad username or password supplied."; 
         unset($_GET['action']); 
        } 
       }else { 
        $_SESSION['error'] = "Username and Password are required to login."; 
        unset($_GET['action']); 
       }   
      break; 
      case 'logout': 
       if (loggedIn()) { 
        logoutUser(); 
        header ("Location: ".$domainNow); 
       }else { 
        unset($_GET['action']); 
       } 
      break; 
     } 
    } 
    if (loggedIn()) { 
     header ("Location: ".$domainNow); 
    }elseif (!isset($_GET['action'])) { 
     $sUsername = ""; 
     if (isset($_POST['username'])) { 
      $sUsername = $_POST['username']; 
     } 

     $sError = ""; 
     if (isset($_SESSION['error'])) { 
      $sError = '<span id="error">' . $_SESSION['error'] . '</span><br />'; 
     } 

     $sOutput .= '<h2>Login to NextGenStep</h2><br /> 
       ' . $sError . ' 
       <form name="login" method="post" action="/user-action/login"> 
        Username: <input type="text" name="username" value="' . $sUsername . '" /><br /> 
        Password: &nbsp;<input type="password" name="password" value="" /><br /><br /> 
        <div style="padding-left:71px;"><input type="submit" name="submit" value="Login!" /></div> 
       </form> 
      <div style="padding-left:131px;"><h4>Create a new <a href="/register">account</a>?</h4></div>'; 
    } 
    echo $sOutput; 
?> 

我新的PHP和MySQL在一起,所以我想在这里是让在用户登录后数据库中的用户标识(列名是uid),但我也希望它将用户标识存储在会话中,以便我可以在其他页面上使用它。我已经session_start();在包含文件中声明,所以我不必在每个页面上手动执行。

而且,这里是我的网页包括我的loggedIn功能:

function loggedIn() { 
if (isset($_SESSION['loggedin']) && isset($_SESSION['username'])) { 
    return true; 
} 
+1

那么,你有一个问题或什么是你的麻烦你是在跑? – Rasclatt 2014-09-29 17:49:28

+0

正如我所说的,我想在用户成功登录后将用户标识存储为会话,但我不知道如何获取它。我试着运行一个简单的mysql查询:SELECT uid FROM users WHERE username =。 $ _SESSION ['username']。 但它并没有很好的工作。 – 2014-09-29 18:01:22

回答

0

只需保存在会话变量,如UID;

$_SESSION['uid'] = $uid; 

记住尝试使用$_SESSION变量之前使用session_start();,或者这将是不确定的。

顺便说一下,您的loggedIn()函数应该有一个虚假的回报以及一个真实的回报。我会用这个;

function loggedIn() { 
    return ((isset($_SESSION['loggedin']) and isset($_SESSION['username'])) ? true : false); 
} 

希望这会有所帮助。

+0

将uid保存为会话变量只会导致我在页面上出现未定义的变量错误。 – 2014-09-29 17:59:07

+0

您需要在每个要使用'$ _SESSION'变量的代码页的顶部开始会话(或者在您的控制器中,如果有的话)。 'session_start();' – worldofjr 2014-09-29 18:01:51

+0

正如我在帖子中提到的,我已经声明了session_start();在每一页上都包含一个php页面。 – 2014-09-29 18:04:23

0

从数据库中检索您的UID,提供它被刻在那儿,而不是空的尝试(当然无论哪种方式,为空或不为空):

class FetchUID 
    { 
     public static function execute($_uid='') 
      { 
       if(!empty($_uid)) { 
         // I am pressuming you have a global database engine 
         // If not, retrieve your engine however at this point. 
         // For sake of ease, I am pretending it's a global.... 
         global $con; 

         // I use PDO so this is how I would get it 
         // I am also making the statement broad just to make sure it is retrieving at all 
         $query = $con->prepare("select * from users where username ='$_uid'"); 
         $query->execute(); 

         if($query->rowCount() == 1) 
          $user_info = $query->fetch(PDO::FETCH_ASSOC); 
         else 
          $user_info['uid'] = self::Write(); 
        } 

       return (isset($user_info['uid']) && !empty($user_info['uid']))? $user_info['uid']:'error'; 

      } 

     protected static function Write() 
      { 
       // Code here that creates the custom uid and returns it again 
      } 
    } 

// Check that the session->username is set + that the session->uid is NOT set 
if(isset($_SESSION['username']) && !isset($_SESSION['uid'])) { 
     // If set, fetch it via above class & methods 
     $uid = FetchUID::execute($_SESSION['username']); 
     // Write your $uid 
     echo $uid; 
     // Sessionize it 
     $_SESSION['uid'] = $uid; 
    }