我正在为网站创建一个登录。 index.php文件通过检查用户详细信息来控制显示的内容。如果用户登录,则显示消息,否则显示登录表单。我已经包含了文件connect.php和core.php。当我打开index.php时,我收到以下错误。变量con已经定义,但它显示它是未定义的,请帮助我找到错误提前谢谢。
错误:
Notice: Undefined variable: con in C:\xampp\htdocs\includes\core.php on line 33
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\includes\core.php on line 33
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\includes\core.php on line 34
的index.php
<?php
require_once 'includes/connect.php' ;
require_once 'includes/core.php' ;
if(logged_in())
{
echo "You are logged in" ;
}
else
{
?>
<form action="login.php" method="post">
<table>
<tr>
<td>Email</td>
<td>Password <a href="resetpass.php">Forgot Password?</a></td>
<td></td>
</tr>
<tr>
<td><input type="email" name="sin_email" placeholder="Email" name="email"></td>
<td><input type="password" name="sin_pass" placeholder="Password" name="pass"></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
<?php
}
?>
包括/ core.php中
<?php
//Start output buffer.Stores all the output of the server into a stack
ob_start() ;
//Start a session
session_start() ;
//Get the name of current file
$current_file=$_SERVER['SCRIPT_NAME'] ;
//Get the referrer to this page if any
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']))
{
$http_referrer=$_SERVER['HTTP_REFERER'] ;
}
function logged_in()
{
if(isset($_SESSION['login_token'],$_SESSION['user_name'],$_SESSION['user_id'])
&& !empty($_SESSION['login_token']) && !empty($_SESSION['user_name']) && !empty($_SESSION['user_id']))
{
//Store session variable to local variables
$user_name=$_SESSION['user_name'] ;
$user_id=$_SESSION['user_id'] ;
$http_agent=$_SERVER['HTTP_USER_AGENT'] ;
$ip_address=$_SERVER['REMOTE_ADDR'] ;
//Recreating the token string
$token_string=$user_name.$user_id.$http_agent.$ip_address ;
//Obtaining the md5 value for the token_string
$obtained_login_token=md5($token_string) ;
//Fetching previous token_string stored in the database
$token_query="SELECT * FROM securelogin WHERE user_id='$user_id' ;" ;
$token_result=mysqli_query($con,$token_query) ;
$token_info=mysqli_fetch_assoc($token_result) ;
$stored_login_token=$token_info['login_string'] ;
if($stored_login_token===$obtained_login_token)
{
return true ;
}
else
{
return false ;
}
}
else
{
return false ;
}
}
?>
包括/ connect.php
<?php
$host_name="localhost" ;
$user_name="root" ;
$password="" ;
$db_name="futsided" ;
$con=mysqli_connect($host_name,$user_name,$password,$db_name)
or
die("Cannot connect to our server :(<br/>Please try again later or contact <a href=\"/report.html\">Administrator</a>") ;
?>
答案 0 :(得分:0)
您必须将变量$ con传输给函数logged_in($ con未全局定义):
在index.php中更改
if(logged_in())
到
if(logged_in($con))
在includes / core.php中更改
function logged_in()
到
function logged_in($con)