我得到undefined logged_in()函数...

时间:2016-05-22 22:17:06

标签: php mysqli

是什么让登录功能不起作用...我一直在尝试somtime但仍然继续得到相同的错误..我的代码如下:

的index.php

<?php include 'includes/overall/header.php'; ?>
                <div class="info">

                </div>
                <div class="login">
                    <?php 
                    if(logged_in() === true){
                      echo 'Logged in'; 
                    }else{
                        include 'includes/login.php';
                    }
                    ?>
                </div>
<?php include 'includes/overall/footer.php'; ?>

users.php

<?php
require('core/database/connect.php');

function logged_in(){
return(isset($_SESSION['user_id'])) ? true : false;
}

function user_exists($username){
global $username;
global $con;
$username = sanitize($username);
$query = mysqli_query($con,
    "SELECT COUNT(`user_id`) as count
    FROM `users` 
    WHERE `username` = '$username'") 
    OR  die(mysqli_error());

$result = mysqli_fetch_assoc($query);
//echo $result['count'];
return ($result['count'] >= 1) ? true : false;
}

function user_active($username){
global $username;
global $con;
$username = sanitize($username);
$query = mysqli_query($con,
    "SELECT COUNT(`user_id`) as count
    FROM `users` 
    WHERE `username` = '$username' 
    AND `active` = 1") 
    OR  die(mysqli_error());

$result = mysqli_fetch_assoc($query);
//echo $result['count'];
return ($result['count'] >=1) ? true : false;
}

function user_id_from_username($username){
global $username;
global $con;
$username = sanitize($username);
    $query = mysqli_query($con,
    "SELECT `user_id` 
    FROM `users` 
    WHERE `username` = '$username'") 
    OR  die(mysqli_error());

$result = mysqli_fetch_assoc($query);
//echo $result['count'];
return ($result['user_id']);
}

function login($username,$password){
global $con;
$user_id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

    $query = mysqli_query($con,
    "SELECT COUNT(`user_id`) as count
    FROM `users` 
    WHERE `username` = '$username' AND `password` = '$password'") 
    OR  die(mysqli_error());

$result = mysqli_fetch_assoc($query);
//echo $result['count'];
return ($result['count'] >=1) ? $user_id : false;
}

?>

的login.php

<?php
include 'core/init.php';

if(empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) || empty($password)){
    $errors[] = 'You need to enter a username and password';
}else if(user_exists($username) === false){
    $errors[] = 'The account dosen\'t exit.Please register';
}else if(user_active($username ) === false){
    $errors[] = 'Please u have not activated ur account';
}else{

    if(strlen($password) > 32){
        $errors[] = 'Password is too long!.Password must be at most 32 characters long';
    }
    $login = login($username,$password);
    if($login === false){
        $errors[] = 'The username name and password is incorrect';
    }else{
        $_SESSION['user_id'] = $login;
        header('Location:index.php');
        exit();
    }
}

}else{
$errors[] = 'No data recieved';
}
include 'includes/overall/header.php';
if(empty($errors) === false){


?>
<div class='error'>
<h2>We tried to log you in, but....</h2>
<?php
echo output_errors($errors);
}
?>
<a href="index.php">Login</a> or <a href="">Create an account</a>
<?php
include 'includes/overall/footer.php';
?>

</div>

的init.php

<?php
session_start();
//error_reporting(0);

require'database/connect.php';
require'function/general.php';
require'function/users.php';

$errors = array();
?>

我的代码如上所述我做错了什么...这让我得到这个错误?我需要一个解决方案。谢谢提前

3 个答案:

答案 0 :(得分:1)

您的查询正在比较mysqli_fetch_assoc($query)的结果并检查它是否等于1. mysqli_fetch_assoc()的返回值是一个数组(特定于关联),它不是数字。数组是包含变量和其他数组的结构。因此,当您进行比较时(数组==数字), you are obtaining a FALSE`值。

我认为您想要做的是查看您是否从查询中获得了一行。在这种情况下,您可以按如下方式重写代码:

function user_active($username){
    global $username;
    global $con;
    $username = sanitize($username);
    $query = mysqli_query($con,
        "SELECT COUNT(`user_id`) as count
        FROM `users` 
        WHERE `username` = '$username' 
        AND `active` = 1") 
        OR  die(mysqli_error());

    $result = mysqli_fetch_assoc($query);
    return ($result['count'] ==1) ? true : false;
}

我在你的sql语句as count中添加了一段代码,以及下面的几行代码。格式化代码以使其更具可读性也是一种很好的做法。

答案 1 :(得分:0)

函数mysqli_fetch_assoc返回ARRAY,你将它与1(=== 1)进行比较......嗯......所以问题出在哪里......;)

答案 2 :(得分:0)

<?php
  require('core/database/connect.php');

function user_exists($username){
  global $username;
  global $con;
  $username = sanitize($username);
  $query = mysqli_query($con,"SELECT COUNT(`user_id`) FROM `users` WHERE     `username` = '$username'") OR  die(mysqli_error());
  return (!empty(mysqli_fetch_assoc($query))) ? true : false;
}