doutang2017 2014-12-23 07:58
浏览 39
已采纳

执行连接到数据库和执行数据库查询时出错

I am creating a log in for a website. index.php file controls what is displayed by checking the user details. It display a message if user is logged in else the login form. I have included the files connect.php and core.php. When I open index.php I get the following error. The variable con is already defined but it shows it is undefined please help me to find the mistake Thanks in advance.

Error:

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
}
?>

includes/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 ;
    }
}
?>

includes/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>") ;
?>
  • 写回答

1条回答 默认 最新

  • duanhuilao0787 2014-12-23 08:18
    关注

    You have to transfer the variable $con to the function logged_in ($con is not globally defined):

    In index.php change

    if(logged_in())
    

    to

    if(logged_in($con))
    

    In includes/core.php change

    function logged_in()
    

    to

    function logged_in($con)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序