使用PDO显示当前用户名

时间:2014-01-11 04:01:06

标签: php mysql

我想使用PDO和$ _SESSION ['uid']来获取当前成员名称并将其显示回来。

CREATE TABLE IF NOT EXISTS `users` (
`uid` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(64) NOT NULL,
`salt` varchar(16) NOT NULL,
`password` varchar(64) NOT NULL,
`name` varchar(255) NOT NULL,
`birthday` date NOT NULL,
`activation` varchar(40) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

上面是表格

<?php
require ('common.php'); // Database connection, Session Starter 
$ps = $db->prepare("SELECT * FROM `users` WHERE uid = :uid");
$ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 0);
$ps->execute();
$ps->setFetchMode(PDO::FETCH_ASSOC);
while ($ps as $row)
{
echo $row['name'];
}
?>

上方显示空白屏幕。

 <?php 
 session_start(); 
 Print_r ($_SESSION);
 var_dump($_SESSION);
 ?> 

Print_R显示数组([uid] =&gt;数组) Var_dump显示array(1){[“uid”] =&gt; string(5)“Array”}

<?php 
require("common.php"); 

if(!empty($_POST)) 
{ 
$query = "SELECT uid, email, password, salt FROM users WHERE  email = :email";  
$query_params = array( ':email' => $_POST['email'] ); 

try { 
$stmt = $db->prepare($query); 
$result = $stmt->execute($query_params); 
} 

catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } 

$login_ok = false; 
$row = $stmt->fetch(); 
if($row) 
{ 
$check_password = hash('sha256', $_POST['password'] . $row['salt']); 
for($round = 0; $round < 222222; $round++) 
{ 
$check_password = hash('sha256', $check_password . $row['salt']); 
} 

if($check_password === $row['password']) 
{  
$login_ok = true; 
} 
} 

if($login_ok) 
{  
unset($row['salt']); 
unset($row['password']); 
$_SESSION['uid'] = $row; 
header("Location: https://**"); 
die("Redirecting to: https://**"); 
} 
else 
{  
header("Location: https://**"); 
die("Redirecting to: https://**"); 
} 
} 
?>

包含login.php

1 个答案:

答案 0 :(得分:0)

更改此行

       $ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 0);

       $ps->bindParam(':uid', $_SESSION['uid'];, PDO::PARAM_STR, 12); //12 means length of the data type