PHP登录会话和回显用户名

时间:2014-12-31 10:37:38

标签: php mysql

问题出现在我的登录页面上,一切正常但这里的问题是当我成功登录用户名时出现在主页上。

include ('connect.php');

if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
    $usernameLog = $_POST['usernameLog'];
    $passwordLog = $_POST['passwordLog'];
    $valid = true;

     if($valid == TRUE){
    include('connect.php');

    $usernameLog = mysql_real_escape_string($usernameLog);
    $passwordLog = mysql_real_escape_string(md5($passwordLog));

    $sql = mysql_query("SELECT * FROM register WHERE username = '$usernameLog' AND passconf = '$passwordLog'");


    if(mysql_num_rows($sql) > 0){

        //if true then lets go to the logged in page
        session_start();

        $_SESSION['logat'] = "da";

        header('location: BlogWorld.php');

我不确定出了什么问题。请帮忙

//为主页编码sesssion,即用户成功登录时

include('connect.php');

// Start session
    session_start();


// Check if user is logged and existing session
    if(isset($_SESSION['username'])) {
            include ("connect.php");

// Content for user logged
    echo "Welcome ".$username." :) - <a href='logout_Blog.php'>Logout</a>";
    } else {
// Redirecting to login page
    Header("Location: ./");

2 个答案:

答案 0 :(得分:0)

在您使用session_start()的页面顶部添加session

并存储

$_SESSION['username'] = $usernameLog;

然后echo $_SESSION['username'];

答案 1 :(得分:0)

Try with this code,

include ('connect.php');

if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
$usernameLog = $_POST['usernameLog'];
$passwordLog = $_POST['passwordLog'];
$valid = true;

if($valid == TRUE){
include('connect.php');

$usernameLog = mysql_real_escape_string($usernameLog);
$passwordLog = mysql_real_escape_string(md5($passwordLog));

$sql = mysql_query("SELECT * FROM register WHERE username = usernameLog' AND      passconf = '$passwordLog'");

$result=mysql_fetch_row( $sql);
if(mysql_num_rows($sql) > 0){

    //if true then lets go to the logged in page
    session_start();

    $_SESSION['username'] = $result[0]['username'];

    header('location: BlogWorld.php');