无回声Session用户php

时间:2013-07-23 15:55:05

标签: php sql session login

if正在运行,但打印的用户名是“username”,它应该是真正的用户名,在这种情况下是“teste”。 也许我需要一个变量或其他东西来显示它但我没有到达那里:s

这是我的会话初始代码:

<?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>

3 个答案:

答案 0 :(得分:4)

您在此处指定文字username

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

那么为什么要显示其他任何东西呢?也许你应该使用

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

同样输入密码字段。作为一般提示,您应 NOT 将密码存储在会话中。

答案 1 :(得分:1)

您将$_SESSION['username']设置为等于文字字符串&#39;用户名&#39;。它应该是$_SESSION['username'] = $username

更新:不要将密码存储为会话。它不是必需的,也不安全。如果以后需要密码,请使用用户名会话作为参考进行SQL查询。

答案 2 :(得分:0)

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

you need to fetch the MySQL data and try using MySQLi