Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中显示登录用户的欢迎用户名_Php_Mysql - Fatal编程技术网

在php中显示登录用户的欢迎用户名

在php中显示登录用户的欢迎用户名,php,mysql,Php,Mysql,这是我的索引。php <?php // Inialize session session_start(); // Check, if username session is NOT set then this page will jump to login page if (!isset($_SESSION['username'])) { header('Location: index.html'); } echo "Welcome", $_SESSION['username']; ?&

这是我的索引。php

<?php

// Inialize session
session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: index.html');
}
echo "Welcome", $_SESSION['username'];
?>


<div id="content">
<p><?php
if(isset($_SESSION['username'])){
    echo "Welcome", $_SESSION['username'];}?>
</p> 
</div>


这是我的check login.php,在我提交详细信息后,它们会出现在这里

<?php

session_start();
ob_start();-
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// 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");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

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

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

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

我试图让登录的用户名出现在index.php的欢迎之后,但它似乎不起作用,我花了几个小时在谷歌上搜索它

$_SESSION['username'] =
$username;
$_SESSION['password'] =
$password;
您应该将此更改为:

$_SESSION['username'] =
$myusername;
$_SESSION['password'] =
$mypassword;

因为$myusername和$mypassword指的是从表单数据发送到脚本的用户名和密码。

有什么特别不起作用?它是在没有用户名的情况下说“欢迎”,还是转到另一个页面?此外,不要无缘无故地将变量放入字符串中<代码>“$host”毫无意义<代码>$host可以。您定义了$username=“”;然后$\u会话['username']=$username;正常情况下,您有一个空字符串否?不是这样,但您正在将
会话
用户名设置为
$username
,而不是
$myusername
。此外,mysql_*函数也不推荐使用。您应该在准备好的语句中使用mysqli或PDO。另一方面,您应该对密码进行散列和盐析,而不是以纯文本形式存储。@MichaelBenjamin您可以使用逗号进行回显。这比连接要快,因为php必须检查数据类型。非常感谢,真不敢相信我漏掉了-\我不是唯一提到它的人,@Matthew Johnson也这么做了。