[英]Display user's name after login using session (php and sqlsrv)
session.php session.php文件
<?php
if (!isset($_SESSION)) {
session_start();
}
if($_SESSION['valid_user']!=true){
header('Location: login.php');
die();
}
?>
connection.php connection.php
<?php
$db_host = '(local)';
$db_user = 'user';
$db_pwd = 'pwd';
$database = 'Demo';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_get_last_message(), true));
}
?>
homepage.php homepage.php
<?php
require_once 'session.php'
require_once 'connection.php';
$tsql = "SELECT * FROM USER WHERE EmployeeId='$EmployeeId'";
$result = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
$row = sqlsrv_fetch_array($result); // ERROR
$EmployeeId = $row['EmployeeId'];
$UserName = $row['UserName'];
sqlsrv_close($conn);
echo 'Welcome ' . $_SESSION['UserName'] . ' | ' . $_SESSION['EmployeeId'] //ERROR
?>
I would like to display the name (UserName) of the user after the user log into the site by using the session. 我想在用户使用会话登录站点后显示用户的名称 (UserName)。 But i can only display the employee id (EmployeeId) as i've created the session from the login_action 但是我只能显示我从login_action创建会话的员工ID (EmployeeId)
login_action.php login_action.php
<?php
require_once 'session.php'
require_once 'connection.php';
$EmployeeId = $_REQUEST['EmployeeId'];
$Password = $_REQUEST['Password'];
$tsql = "SELECT * FROM LOGIN WHERE EmployeeId='$EmployeeId' AND Password='$Password'";
$result = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
$num = sqlsrv_num_rows($result);
if($num==1){
header('Location: homepage.php');
$_SESSION['valid_user'] = true;
$_SESSION['EmployeeId'] = $EmployeeId;
die();}
else
header('Location: login_error.php');
die();
?>
Could you help me on how can i display the user's name as i got these errors : 当出现这些错误时,您能帮我如何显示用户名:
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given in homepage.php line 22
AND 和
Notice: Undefined index: UserName in homepage.php line 33
I've searched everywhere and i can't seem to find the answer. 我到处搜索,但似乎找不到答案。
I've found the solution months ago. 我几个月前找到了解决方案。 But decided to post to share with other readers who've been experiencing the same problem as i am. 但是决定发帖与其他遇到与我同样问题的读者分享。
session.php session.php文件
<?php
if (!isset($_SESSION)) {
session_start();
}
if($_SESSION['valid_user']!=true){
header('Location: login.php');
die();
}
?>
connection.php connection.php
<?php
$db_host = 'local';
$db_user = 'root';
$db_pwd = 'pwd';
$database = 'MyDB';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_get_last_message(), true));
}
?>
login_action.php login_action.php
<?php
session_start();
require_once 'connection.php';
$EmployeeId = $_POST['EmployeeId'];
$Password = $_POST['Password'];
$UserType = $_POST['UserType'];
$tsql = "SELECT * FROM [MyDB].[dbo].[LOGIN] WHERE EmployeeId='$EmployeeId' AND Password='$Password' AND UserType='$UserType'";
$result = sqlsrv_query( $conn, $tsql,$params , array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
$num = sqlsrv_num_rows($result);
if($num==1){
$_SESSION['valid_user'] = true;
$_SESSION['EmployeeId'] = $EmployeeId;
$_SESSION['UserType'] = $UserType;
if($UserType == "USER")
{
header("Location: user.php");
exit();
}
else if ($UserType == "SUPERIOR")
{
header("Location: superior.php");
exit();
}
else if ($UserType == "ADMIN")
{
header("Location: admin.php");
exit();
}
else
die("Not a Valid User Type.");
}
else
//die(sqlsrv_errors());
header('Location: login_error.php');
exit();
?>
index.php 的index.php
<?php
$EpId = $_SESSION['EmployeeId'];
$tsql =
"SELECT *
FROM [MyDB].[dbo].[USER]
WHERE EmployeeId = '$EpId'";
$result = sqlsrv_query($conn, $tsql, array(), array ("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row=sqlsrv_fetch_array($result))
{
$EId = $row['EmployeeId'];
$uName = $row['UserName'];
}
echo 'Welcome $uName | $EpId';
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.