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 : An Authenticator with Cookies and Redirect
Categories : Authentication, PHP Click here to Update Your Picture
Sheridan Saint-Michel
Date : Feb 24th 2001
Grade : 2 of 5 (graded 20 times)
Viewed : 22805
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sheridan Saint-Michel
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php

//Put in your own info for username, password, DB, email@address, Cookiename,
//the name of this page (currently login.php) and the name of your subscribe
//or new user page (currently new.php). I went ahead and included all the HTML
//so this page should work as is, with only the changes described above needed
// - Sheridan Saint-Michel (Lysander@onlychildclub.com)

$dblink = mysql_pconnect("localhost","username","password");
mysql_select_db("DB");

$headers=0; //Make Sure HTML Headers are in place before the form


//after Authenticating the script automatically sends the browser to
//the webpage of your choice (note if your page calls this
//script with ?redirect="foobar.php" it will automatically
//redirect to foobar.php after authenticating. Set the default
//redirect page here

if ( !isset($redirect))
{
$redirect = "index.php";
}

if (isset($UserID) && isset($Password)) {

$query = "select * from members where UserID = \"$UserID\" and Password =
\"$Password\"";

if ( !($dbq = mysql_query($query, $dblink))) {
echo "Unable to query database. Please Contact <a
href=\"mailto:email@address\">email@address</a>.\n";
exit;
}

$lim = mysql_num_rows( $dbq );

if ($lim != 1) {

$headers=1; //HTML headers in place
echo "<HTML><HEAD><TITLE>Login Page</TITLE></HEAD><BODY>";
echo "<B>Invalid User ID or Password. Please Try again</B><BR>";

}

if ($lim == 1) {

//make unique session id and store it in Database
$timer = md5(time());
$sid = $UserID . "+" . $timer;
SetCookie("Cookiename",$sid,time()+2592000); //Set Cookie for 30 days
$query = "update members set sid=\"$timer\" where UserID=\"$UserID\"";

if( !($dbq = mysql_query( $query, $dblink))) {
echo "Unable to update database. Please contact <a
href=\"mailto:email@address\">email@address</a>.\n";
exit;
}

$headers=1;
header("Location: $redirect");
exit;
}

}

if (isset($Cookiename)) {
$headers=1; //make sure HTML headers are in place before the form
$sidarray = explode("+", "$Cookiename");
$query = "select * from members where UserID = \"$sidarray[0]\" and sid = \"$sidarray[1]
\"";

if ( !($dbq = mysql_query($query, $dblink))) {
echo "Unable to find database. Please Contact <a
href=\"mailto:email@address\">email@address</a>.\n";
exit;
}

if (mysql_num_rows( $dbq ) == 1) {
echo "<HTML><HEAD><TITLE>Login Page</TITLE></HEAD><BODY>";
echo "You are already logged in as $sidarray[0].<BR>";
echo "You may logon as another user or simply begin using our services with your current
session.<BR>";
echo "Click <A Href=\"http://www.mydomain.com/home.php\">Here</A> to return to our
homepage.";
}
}

if ($headers == 0) {
echo "<HTML><HEAD><TITLE>Login Page</TITLE></HEAD><BODY>";
}

echo "<Form Action=\"login.php\" METHOD=POST>";
echo "<H2>User Name</H2>";
echo "<Input TYPE=\"text\" Name=\"UserID\" Value=",$UserID,">";
echo "<BR>";
echo "<H2>Password</H2>";
echo "<Input TYPE=\"password\" Name=\"Password\">";
echo "<BR>";
echo "<Input Type=\"submit\" Value=\"Submit\">";
echo "<Input Type=\"hidden\" Name=\"redirect\" Value=\"$redirect\">";
echo "</FORM>";
?>
<A HREF=new.php>Create an Account</A>
</BODY>
</HTML>



<?php
//Header for Authenticator with Cookies:
//I received some e-mail asking what code should be placed on other pages of the website using my Authenticator
//with Cookies and Redirect. This should appear before the HTML Tag on any page you want protected.

//Put in your own info for username, password, DB, email@address, Cookiename,
//the name of this page (currently thispage.php), and the name of the login page (currently
login.php).
//Cookiename MUST be the same as Cookiename in the login page.

$dblink = mysql_pconnect("localhost","username","password");
mysql_select_db("DB");

if( !(isset( $CookieName )))
{
header("Location: http://www.yourdomain.com/login.php3?redirect=thispage.php");
exit;
}

$sidarray = explode("+","$CookieName");

$query = "select * from members where UserID = \"$sidarray[0]\" and sid = \"$sidarray[1]\"";

if ( !($dbq = mysql_query( $query, $dblink))) {
echo "Unable to find database. Please Contact <A
HREF=\"mailto:email@address\">email@address</a>.\n";
exit;
}

if (mysql_num_rows( $dbq ) != 1) {
header("Location: http://www.yourdomain.com/login.php3?redirect=thispage.php");
exit;
}

?>






Simple Password example
Categories : PHP, Authentication, Security, HTTP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
MD5 secured login
Categories : PHP, Java Script, Authentication, Security
Import the yahoo address book.
Categories : PHP, CURL, Authentication
A simple PHP login script that you can modify to suite your needs. It use a session to store data in a session file submited by the page.
Categories : PHP, Sessions, Security, Authentication
HTTP Basic Authentication via POP3.
Categories : Authentication, HTTP, Email, PHP
PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
SQL / PHP based Integrated Authentication
Categories : PHP, Authentication, Databases, MySQL
user-authentication with php3 and msql
Categories : Authentication, mSQL, PHP, Databases
Access_user Class - an easy to use system for protecting pages and register users.
Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication
Authentication script to authenticate users in Active Directory through LDAP.
Categories : LDAP, Authentication, Cookies, PHP
.htpassword manager for apache
Categories : PHP, PHP Classes, Authentication, Apache
Implementing a Members ONLY area
Categories : PHP, MySQL, Databases, Authentication
Authentication HTTP protocol POST
Categories : Authentication, HTTP, PHP
 Craig Horchem wrote : 754
Hello,

    I have downloaded your script and it has worked flawless freestanding. However, I do use it in two folders, each authenticating from a table in the same database, and keep getting weird occurences. Such as the page will just not display blank. I did only modify one thing which shouldn`t affect this.

Any suggestions, conclusions??? I don`t really understand why it will not work in two different folders.

 
 James Reimer wrote :845
I might suggest changing

$query = "select * from members where UserID = \"$UserID\" and Password = 
\"$Password\"";

to

$query = "select id from members where UserID = \"$UserID\" and Password = 
\"$Password\"";

to avoid passing the password around.  It should still function correctly.