gpt4 book ai didi

php - 无法在写入上下文 Php 使用 $_Session 中使用函数返回值

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:48 25 4
gpt4 key购买 nike

我正在为我正在使用的网站开发登录功能,我正在尝试使用 session 方法,但我一直收到错误“ fatal error :无法在写入上下文中使用函数返回值。 “有什么想法吗?

<?php
$db_name="name"; // Database name
// Connect to server and select databse.
mysql_connect("xxxxxxx","xxxx","xxxxxx")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)
$myusername = stripslashes($username);
$mypassword = stripslashes($password);
$myusername = mysql_real_escape_string($username);
$mypassword = mysql_real_escape_string($password);
$sql1="SELECT * FROM `professionals` WHERE username='$username'";
$sql2="SELECT * FROM `professionals` WHERE password='$password'";
$result1=mysql_query($sql1);
$result2=mysql_query($sql2);

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

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
$_SESSION("username")=$result1;
$_SESSION_("password")=$result2;
header("location:Success.html");
}
else {
echo "Wrong Username or Password";
header("location:Failed.html");
}
?><?php
$db_name="name"; // Database name

// Connect to server and select databse.
mysql_connect("xxxxxxx","xxxx","xxxxxx")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)
$myusername = stripslashes($username);
$mypassword = stripslashes($password);
$myusername = mysql_real_escape_string($username);
$mypassword = mysql_real_escape_string($password);
$sql1="SELECT * FROM `professionals` WHERE username='$username'";
$sql2="SELECT * FROM `professionals` WHERE password='$password'";
$result1=mysql_query($sql1);
$result2=mysql_query($sql2);

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

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){ //Write Username and Password goes to page
$_SESSION("username")=$result1;
$_SESSION_("password")=$result2;
header("location:Success.html");
}
else {
echo "Wrong Username or Password"; //Wrong Password goes to error page
header("location:Failed.html");
}
?>

最佳答案

$_SESSION 是数组,不是函数。您在这里尝试将其用作后者:

$_SESSION("username")=$result1;
$_SESSION_("password")=$result2;

您需要按如下方式更新您的代码:

$_SESSION["username"] = $result1;
$_SESSION["password"] = $result2;

您的代码还有许多其他错误:

  1. mysql_*() 函数现已弃用。你应该考虑升级您的代码以利用 MySQLiPDO .

  2. 我看不到任何对 session_start() 的调用在你的代码中。这是当您希望使用用户 session 时需要。

关于php - 无法在写入上下文 Php 使用 $_Session 中使用函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21343980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com