WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP3 Authentication using MySQL (also has nice query_db() function to return an array of arrays from a MySQL query result).
Categories : Authentication, MySQL, PHP Update Picture
Tom Ward
Date : Mar 03rd 2000
Grade : 4 of 5 (graded 5 times)
Viewed : 16921
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Tom Ward
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?
// All of this SHOULD go into an .inc file

function query_db($query) {
        // connect and get row(s)
        $mysql = mysql_pconnect('host', 'sqluser', 'sqlpass');
        $success = mysql_select_db('database');
        $result = mysql_query($query, $mysql);

        if ($err) {
                // return empty array :P
                echo "Error querying database<BR>\n";
                echo "$err<BR>\n";
                echo "Please alert <A HREF=\"root@yoursite.com\">admin</A><BR>\n";
                return array();
        } else {
                // make the array
                $ret_array = array();

                // do we have something?
                if (mysql_num_rows($result) > 0) {

                        // yes get each one and stuff it in
                        while ($row = mysql_fetch_array($result)) {
                                $ret_array[] = $row;
                        }
                }

                // returns arrays of rows, or array() if no rows
                return $ret_array;
        }
}

function authenticate() {
        // not authenticated, or previously failed authentication
        Header('WWW-authenticate: basic realm="Realm name");
        Header('HTTP/1.0 410 Unauthorized');
        echo "You must have a valid login and password to access this page\n";
}

function LoginIsValid($uname, $passwd) {
        // don't even bother if they're empty
        if ($uname == "" | $passwd == "")
                return 0;
                // query the database
        $query = "SELECT userid,password FROM users WHERE username='$uname'";
        $result = query_db($query);
                // did we get anything back
        if (count($result) != 0) {
                // let's get 'each one' (just for showing how to get multiple rows w/ my query_db)
                while (list($key, $arr) = each($result)) {
                        $row = $arr;                        // get a row
                        $uid = $row['userid'];
                        $pass = $row['password'];
                        if ( (crypt($passwd, $pass) == $pass) and ($pass != "") ) {
                                return $uid;        // valid login
                        } else {
                                return 0;        // invalid login
                        }
                }        // end while()
        } else {
                return 0;                        // not even a user by that name!
        }        // end if (count($result) != 0)
}        // end function

?>

<?
        // THIS part goes in your page
        require('db.inc');         // or where ever you have the above code
        if (LoginIsValid($PHP_AUTH_USER,$PHP_AUTH_PW) == 0)
                authenticate();
        // if I need an else or the like, let me know!
?>



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
SQL / PHP based Integrated Authentication
Categories : PHP, Authentication, Databases, MySQL
Implementing a Members ONLY area
Categories : PHP, MySQL, Databases, Authentication
Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database.
Categories : PHP, Cookies, MySQL, HTML and PHP, Authentication
Password protection for Phorum 3.1.x with userlevels and log.
Categories : PHP, MySQL, Authentication, Security
PHP4 AND MySQL Authentication
Categories : PHP, MySQL, Authentication, Databases
Full membership authentication system.
Categories : Authentication, MySQL, PHP, Databases
PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL
complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers.
Categories : Authentication, Complete Programs, PHP, MySQL, Databases
Function to do live population of HTML's <Select> tag from a Table
Categories : PHP, MySQL, HTML and PHP, Databases
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
This script is a contact form between users of a website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps
PHP3 generated gif / javascript mouseover.
Categories : PHP, Java Script, MySQL
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
 Tom Ward wrote : 260
Found 2 bugs:
1) line saying `if ($uname == "" | $passwd == "")` should be
changed. Either use `or`, or `||` since `|` is a bitwise
operation.

2) The last lines should be...
if (LoginIsValid($PHP_AUTH_USER,$PHP_AUTH_PW) == 0) {
    authenticate();
    exit;
}

Then below that you can have the rest of your HTML or PHP to
execute
 
 Brian J wrote : 416
It amazes me how many developers just use 
something along the lines of:

if($var == ``)

To check for an empty value. Yet, if someone just 
hits the spacebar, the above will let it slide. 

Whitespace has value people.

In PHP, use trim() to hack off whitespace, then use 
empty(). In Perl use something like:

if($var =~ /^\s*$/)

 
 Raoul Zon wrote : 609
I`ve tried this script and several others, but I keep getting this ignorant warning: "cannot add header information - headers already sent by ("file") on line 21"

Can anyone tell me how to get rid of it???
Thanks in advance!
Regards,
Raoul
 
 yvette hirth wrote :1332
typo:  in the line 
Header(`WWW-authenticate: basic realm="Realm name");
he`s missing the final ` before the closing parend.