Jump to content

Where's the error?


LemonInflux

Recommended Posts

Basically, this is a login code for my in-the-works forum.

 

This is user_auth_fns.php (the file with the function):

 

<?php
require_once('db_fns.php');
db_connect();

function login($username, $password){
$sql = 'SELECT * FROM `members` where username = "'. $username .'" and password = "sha1('. $password .')"';
$sqlresult = mysql_query($sql);
if(mysql_num_rows($sqlresult) = 0){
return false;
}elseif(mysql_num_rows($sqlresult = 1){
return true;
}else{
return false;
}
}
?>

 

Here is db_fns.php:

 

<?php
function db_connect(){
$dbHost = "x";         //Location Of Database
$dbUser = "x";         //Database User Name
$dbPass = "x";        //Database Password
$dbDatabase = "x"; //Database Name

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); 
}
?>

 

 

And here is the start of index.php (the main file):

 

<?php
include('db_fns.php');
include('user_auth_fns.php');
db_connect();

session_start();

//create short variable names
$username = $_POST['username'];
$password = $_POST['password'];

if(isset($username) && isset($password))
// they have just tried logging in
{
  login($username, $password);
    // if they are in the database register the user id
    $_SESSION['valid_user'] = $username;
  $message = '- Login Successful!';
  }else{
  $message = '- Login Failed!';
}
?>

 

My problem is, it's causing index.php to be completely blank. So, where's the error? I think I've narrowed it down to a problem in user_auth_fns.php, because when I delete the include, it works fine. Any help is appreciated. Thanks in advance, Tom.

Link to comment
Share on other sites

  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

It would be a good idea to turn display_errors on and up error_reporting to E_ALL in the php.ini. That way if there is any errors they be shown during run time rather than getting an unhelpful blank screen.

 

But for now try removing these two lines:

require_once('db_fns.php');
db_connect();

in user_auth_fns.php.

 

You have already included db_fns.php in index.php. No need to include it again in user_auth_fns.php.

Link to comment
Share on other sites

Ah, ok. Also, added that, now these are the errors:

 

 

Notice: Undefined index: username in e:\domains\r\reflexprojects.net\user\htdocs\forumbuild\index.php on line 14

 

Notice: Undefined index: password in e:\domains\r\reflexprojects.net\user\htdocs\forumbuild\index.php on line 15

 

Well, these are:

 

$username = $_POST['username'];
$password = $_POST['password'];

 

But they're only defined if a person is directed to that page via the login.

 

Notice: Undefined index: SESSION_UNAME in e:\domains\r\reflexprojects.net\user\htdocs\forumbuild\inc\header.inc on line 3

 

That's

if(!$_SESSION["SESSION_UNAME"]){

But the header.inc is for the nav stuff. I guess it's talking about the bit I tried to add:

 

if(!$_SESSION["SESSION_UNAME"]){
echo '<a href="login.php">Log In</a></p>';
}else{
echo '<a href="#">Log Out ['. $username .']</a></p>';
}

 

It was meant to change to log in/log out, depending on whether you were logged in or not.

 

 

Notice: Undefined index: SESSION_UNAME in e:\domains\r\reflexprojects.net\user\htdocs\forumbuild\index.php on line 90, which is:

 

<?php if(!$_SESSION["SESSION_UNAME"]){ echo 'You are not logged in. <a href="login.php">Log In</a> or Register.'; }else{ echo 'Hello, '. $username; } ?>

Link to comment
Share on other sites

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

include('config.inc.php');
include('db_fns.php');
include('user_auth_fns.php');
db_connect();

session_start();

//create short variable names

$username =  isset($_POST['username']) ? $_POST['username'] : null;
if (!is_null($username)) 
{
	$username= $_POST['username'];
}
$password =  isset($_POST['username']) ? $_POST['username'] : null;
if (!is_null($username)) 
{
	$username = $_POST['username'];
}


if(isset($username) && isset($password))
// they have just tried logging in
{
  login($username, $password);
    // if they are in the database register the user id
    $_SESSION['valid_user'] = $username;
  $message = '- Login Successful!';
  }else{
  $message = '- Login Failed!';
}
?>

 

It's all in the index.php bit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.