ok so here is the code that is being called

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

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

    if(empty($username) === true || empty($password) === true)
    {
        $errors[] = 'please enter a username and password';
    }else if(user_exists($username) === false)
    {
        $errors[] = 'we can\'t find that user, please contact AZ Media Production';
    }else
    {
        $login = login($username, $password);

        if($login == false)
        {
            $errors[] = 'That username/password combination is incorrect';
        }else
        {
            $_SESSION[`user_id`] = $login;

            header('Location: anounceEdit.php');
            exit();
        }
    }
    print_r($errors);
}
?>

now inside of my init.php is the code calling this code page

<?php
//include '../database/conection.php';
//include 'general.php';}these lines are commented out because of a failed attempt to fix my issue

function user_exsists($username)
{
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `userName` = '$username'");

    return(mysql_result($query, 0) == 1) ? true : false;
}

function user_id_from_username($username)
{
    $username = sanitize($username);
    $query = mysql_query("SELECT `user_id` FROM `users` WHERE `userName` = '$username'");

    return mysql_result($query, 0, 'user_id');
}

function login($username, $password)
{
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $password = sha1($password);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `userName` = '$username' AND `password` = '$password'");

    return(mysql_result($query, 0) == 1) ? $user_id : false;
}
?>

i have tried entering

error_reporting(E_ALL);
ini_set('display_errors', 1);

to see if there are any other errors at the root of it all but i still just get:
Fatal error: Call to undefined function user_exists() in /home/traevale/public_html/riseandshine.com/test/login.php on line 12

I know that the function exists and the call looks correct, anyone else see any problem with my code?

Recommended Answers

All 13 Replies

Your function is named wrong.
Named function: user_exsists
Used function: user_exists

Just a typo.

thanks i have been looking at this code for HOURS!!!!

ok changed that over and I still get the same error only it now says user_exists in stead of user_exsists

i have even created the called function on the login.php and it still gives:

Fatal error: Call to undefined function user_exists() in /home/root/public/client.com/core/login.php on line 12

Member Avatar for diafol

Strange, it works for me. I just did this:

//require 'core/init.php';
    $username = 'u';
    $password = 'p';
    if(empty($username) === true || empty($password) === true)
    {
        $errors[] = 'please enter a username and password';
    }else if(user_exists($username) === false)
    {
        $errors[] = 'we can\'t find that user, please contact AZ Media Production';
    }else
    {
        $login = login($username, $password);
        if($login == false)
        {
            $errors[] = 'That username/password combination is incorrect';
        }else
        {
            $_SESSION[`user_id`] = $login;
            header('Location: anounceEdit.php');
            exit();
        }
    }
    print_r($errors);


//include '../database/conection.php';
//include 'general.php';}these lines are commented out because of a failed attempt to fix my issue
function user_exists($username)
{
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `userName` = '$username'");
    return(mysql_result($query, 0) == 1) ? true : false;
}
function user_id_from_username($username)
{
    $username = sanitize($username);
    $query = mysql_query("SELECT `user_id` FROM `users` WHERE `userName` = '$username'");
    return mysql_result($query, 0, 'user_id');
}
function login($username, $password)
{
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $password = sha1($password);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `userName` = '$username' AND `password` = '$password'");
    return(mysql_result($query, 0) == 1) ? $user_id : false;
}

I took off the post bit and appended the init code. I got a fail on the sanitize function, which shows that the user_exists fired (error line was inside the user_exists function). I'm assuming your require reference is correct for the code and that you're not referencing an older file without that code in it or user_exsists in it.

Ok, it's been a while since I coded PHP, but I'd do the following tests:

  1. Move the function from the init.php file to the login.php file
  2. Empty the function, make it only return true or false.

If the first test is successful it means that is something wrong with the path of the file your are importing or another error is causing this one.

If only the second test is successful it means that the code inside the function is wrong.

If none of them work, let me know and I'll try to think of something else =)

nope still returns the same error

Even with the function in the same file and only returning true? Well, that's really strange.

Just to make sure, remove the import of init.php

Try this and see if works:

<?php

function user_exists($username)
{
    return true;
}

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

    if(empty($username) === true || empty($password) === true)
    {
        $errors[] = 'please enter a username and password';
    }else if(user_exists($username) === false)
    {
        $errors[] = 'we can\'t find that user, please contact AZ Media Production';
    }else
    {
        $login = login($username, $password);

        if($login == false)
        {
            $errors[] = 'That username/password combination is incorrect';
        }else
        {
            $_SESSION[`user_id`] = $login;

            header('Location: anounceEdit.php');
            exit();
        }
    }
    print_r($errors);
}
?>

ok that worked and it moved on to an error created due to the removal of the init.php

ok the problem was syntax, the tutorial I was working with was using what he calls backtags and they were causing the errors. the "backtag" is the symbol under the ~ on the keyboard

Good to hear it.

So mark as solved please =)

i normally do i just wanted to make sure i didnt have any other problems with the code before I mark it solved. =)

commented: Hello I think we were using the same Tutorial, I had the same problem and even did as u say , remove the backtags , but still the same problem ,undefined function error, really dont know what to do. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.