当我尝试登录'
时,我一直收到此错误注意:第6行欢迎数组__________中的数组到字符串转换。
似乎数组导致问题,我登录时希望它显示用户的名字和姓氏。这是我的代码,提前感谢大家:)
Login.php
<?php
require_once('connect.php');
include('includes/head.php');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" >
<div class="container">
<fieldset>
<h2>Login</h2>
<div class="row">
<label class="fixedwidth">Username:</label>
<input type="text" name="username" required />
</div>
<div class="row">
<label class="fixedwidth">Password:</label>
<input type="password" name="password" />
</div>
<div class="row">
<input type="submit" name="submit" value="LogIn" />
</div>
</fieldset>
</div>
</form>
<?php
if(isset($_POST['submit'])) {
require_once('connect.php');
$username = mysqli_real_escape_string($connection, $_POST['username']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
if(!empty($username) && !empty($password)) {
$query = "SELECT * FROM user where username='$username' and
password='$password'";
$result = mysqli_query($connection, $query);
$row=mysqli_fetch_array($result);
mysqli_close($connection);
if(mysqli_num_rows($result) ==1) {
$fullname=array(firstname=>$row['firstname'],lastname=>$row['lastname']);
session_start();
$_SESSION['user'] = $fullname;
header('Location: welcome.php');
}
else {
echo "<p>Could not find you in the database.</p>";
}
}
else {
echo "<p>Either the username or password are invalid. Please try
again</p>";
}
}
?>
Logout.php
<?php
session_start();
unset($_SESSION['user']);
// remove all session variables
session_destroy();
// destroy the session
header('Location: welcome.php');
?>
welcome.php
<?php
include('includes/head.php');
include('includes/nav2.php');
session_start();
echo 'Welcome '. $_SESSION['user'];
echo '<br><br>';
?>
答案 0 :(得分:0)
您所做的就是将数组放入$ _SESSION [&#39; user&#39;]。如果你试图将数组转换为字符串,那么它就不会像在JavaScript中一样工作。
您需要组合数组中的名字和姓氏:
echo 'Welcome ' . $_SESSION['user']['firstname'] . ' ' . $_SESSION['user']['lastname']
答案 1 :(得分:0)
在welcome.php文件中,您已写过
echo 'Welcome '. $_SESSION['user'];
但是变量$ _SESSION ['user']是一个你试图用字符串连接的数组。这是主要问题。数组不能与字符串连接。
所以要解决这个问题,你可以这样做
echo 'Welcome '. implode(" ", $_SESSION["user"]);
希望这有帮助。
答案 2 :(得分:0)
首先你需要从那里删除mysqli_close($connection);
。你可以把它放在脚本的末尾。因为你试图在这里关闭连接,之后你也试图计算行数。可能会造成问题。
2 - 你有阵列。你正在尝试使用out extraction.see
$fullname=array(firstname=>$row['firstname'],lastname=>$row['lastname']);
3-i我建议你从代码中删除它
$fullname=array('firstname'=>$row['firstname'],'lastname'=>$row['lastname']);
并尝试这样。
$_SESSION['user'] = $row['firstname'];
答案 3 :(得分:0)
你不能echo
一个阵列
所以你必须implode
这样的数组:
echo 'Welcome '.implode(" ", $_SESSION['user']);
但是,当您退出时Warning: implode(): Invalid arguments passed in welcome.php
unset
,这将为您提供$_SESSION['user']
。
所以你必须像isset
一样检查它:
echo isset($_SESSION['user']) ? 'Welcome '.implode(" ", $_SESSION['user']) : "Whatever you want when user is not logged in";