TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-03-2008, 06:34 PM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default Checking empty field then connect

I'm having issues figuring out what the problem is. I have a log in form that checks to see if the fields are empty, if they are, then it throws an error out. If they put something in both fields, it checks to see if they are a valid in the database.

PHP Code:
function db_connect() {
    
$dbconn = @mysql_connect('localhost''username''password') or die('Unable to connect to database at this time, please try again later.');
    
$select = @mysql_select_db('dbname') or die('Unable to select database, please try again later.');
}

function 
login($username$password){
    
$error = array();
    if (
count($_POST) > 0) { // assume the form has been submitted
    
if (empty($_POST['username']) || empty($_POST['password'])) $error[] = 'A valid username and password is needed';
        if (
count($errors)) {
    echo 
"$errors";
    }
    
    }else { 
//Check username and password
    
$conn db_connect();
    
$result $conn->query("SELECT * FROM admin WHERE username='$username' AND password=sha1('$password')");
    if (!
$result){
        die(
'Unable to connect to database at this time, please try again later.');
    }
    }

Whats happening is that my script is automatically trying to connect to my database when I load the page. I want it to run when the user hits "login".

In my HTML I have
Code:
<?php
login('username', 'password')
?>
What could I possibly be doing wrong ?
9three is offline  
Reply With Quote
Old 02-06-2009, 10:15 PM   #2 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 13
Thanks: 0
trmbne2000 is on a distinguished road
Default

The problem with your code is the if (count($errors)) { on line 4 of the login function. Your error array is $error earlier in the code.

I see that you are passing in the username and password arguments, yet you are still looking for the POST values in the function. It may be better to use:
Code:
function login($username, $password){
    $error = array();
    if (empty($username) || empty($password)) {
        echo 'A valid username and password is needed';
    } else {
        $conn = db_connect();
        $result = $conn->query("SELECT * FROM admin WHERE username='$username' AND password=sha1('$password')");
        if (!$result){
            die('Unable to connect to database at this time, please try again later.');
        }
    }
}
AND
Code:
  if (isset($_POST['username'])) {
    login($_POST['username'],$_POST['password']);
  }
Not sure if you only gave a partial example of your db_connect() method, but you may need to look at that as well (no reference to the db object is returned, so you may not be able to query it).

--Andrew
trmbne2000 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
writing field data into the url? sarmenhb Absolute Beginners 4 09-26-2008 03:01 AM
Refresh an input field delayedinsanity Javascript, AJAX, E4X 5 07-27-2008 07:48 AM
Comparing a timestamp field delayedinsanity MySQL & Databases 2 06-02-2008 09:49 PM


All times are GMT. The time now is 07:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design