2013-07-23 33 views
0

如果正在工作,但打印的用戶名是「用戶名」,它應該是真正的用戶名,在這種情況下是「teste」。 也許我需要一個變量或東西后顯示,但我沒有收到有:■不能回顯會話用戶php

這是我的會話啓動代碼:使用

<?php 
session_start(); 
ob_start(); 
$host="localhost"; // Nome do Host 
$username="segu24img"; // Mysql username 
$password="desny"; // Mysql password 
$db_name="segurao_imagem"; // Nome base de dados 
$tbl_name="users"; // Nome da tabela 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$username = stripslashes($username); 
$password = stripslashes($password); 
$username = mysql_real_escape_string($username); 
$password = mysql_real_escape_string($password); 

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $username and $password, table row must be 1 row 

if($count==1){ 

// Register $username, $password and redirect to file "login_success.php" 
$_SESSION['username'] = 'username'; 
//session_register("username"); 
$_SESSION['password'] = 'password'; 
//session_register("password"); 
header("location:login_sucess.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 

林這個代碼,以顯示會話的用戶名:

<?php 

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

       </p> 

       <p>Bem Vindo<u><?php echo $_SESSION['username']; ?></u>- <a href="logout.php">Logout</a></p> 

<?php } else{ ?> 

    <p><a href="#login-box" class="login-window">Login</a></p> 

<?php } ?></p> 
+0

當你print_r($ _ SESSION)時你會得到什麼? – Dylan

+0

你會得到什麼?一個錯誤?一個警告? – Memolition

+0

也可能是明智的刪除您的mysql用戶名/密碼:) – Dylan

回答

4

你指定字面字username這裏:

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

何必什麼都曾經得到的顯示?也許你應該使用

$_SESSION['username'] = $username; 
         ^^^^^^^^^ 

同上的密碼字段。作爲一般提示,您應該在會話中存儲密碼NOT

1

您正在設置$_SESSION['username']等於文字字符串'username'。它應該是$_SESSION['username'] = $username

更新:不要將密碼存儲爲會話。這不是必需的,也不安全。如果稍後需要密碼,請使用用戶名會話作爲參考進行SQL查詢。

0
$_SESSION['username'] = 'username'; 
//session_register("username"); 
$_SESSION['password'] = 'password'; 

you need to fetch the MySQL data and try using MySQLi