Well today I was recording another video for my YouTube account, but the video was too long to upload so I couldn't upload it so I'm deciding to just give out the codes rather than just keep them.
You don't need to change much apart from db.php with your MySQL database configuration details.
Hope it helps, and yes -- there are plenty of these scripts lying around, I just wanted to make one for a video.
SQL
db.php
index.php
login.php
register.php
logout.php
Enjoy,
- m0nsta.
You don't need to change much apart from db.php with your MySQL database configuration details.
Hope it helps, and yes -- there are plenty of these scripts lying around, I just wanted to make one for a video.
SQL
PHP:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`password` text NOT NULL,
`email` varchar(255) NOT NULL,
`ip` varchar(25) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
db.php
PHP:
<?php
@mysql_connect( "localhost", "user", "pass" ) or die( "Can not connect." );
@mysql_select_db( "db_name" ) or die( "Can not open DB" );
session_start();
?>
index.php
PHP:
<?php
include( "db.php" );
if ( isset( $_SESSION['loggedin'] ) == "1" )
{
echo "You are logged in. <a href=\"logout.php\">Logout</a>";
}
else
{
echo "You are not logged in. <a href=\"login.php\">Login</a> or <a href=\"register.php\">Register</a>";
}
?>
login.php
PHP:
<?php
include( "db.php" );
if ( isset( $_SESSION['loggedin'] ) == "1" )
{
echo "You are already logged in. <a href=\"index.php\">Go home</a>";
}
else
{
if ( isset( $_POST['login'] ) )
{
$username = strip_tags( mysql_real_escape_string( $_POST['username'] ) );
$password = md5( strip_tags( mysql_real_escape_string( $_POST['password'] ) ) );
if ( empty( $username ) || empty( $password ) )
{
echo "Enter both fields.";
}
else
{
$userQ = mysql_query( "SELECT * FROM users WHERE `username` = '{$username}'" );
if ( mysql_num_rows( $userQ ) == 0 )
{
echo "This user does not exist.";
}
else
{
$userA = mysql_fetch_array( $userQ );
if ( $password !== $userA["password"] )
{
echo "The password is incorrect but the user exists.";
}
else
{
$_SESSION['loggedin'] = "1";
header( "Location: index.php" );
exit;
}
}
}
}
?>
<form method="post">
Username: <input type="text" name="username" maxlength="25" /><br />
Password: <input type="password" name="password" maxlength="20" /><br />
<input type="submit" name="login" value="Login" />
</form>
<?php
}
?>
register.php
PHP:
<?php
include( "db.php" );
if ( isset( $_SESSION['loggedin'] ) == "1" )
{
echo "You are already logged in/registered. <a href=\"index.php\">Go home</a>";
}
else
{
if ( isset( $_POST['register'] ) )
{
$username = strip_tags( mysql_real_escape_string( $_POST['username'] ) );
$password = strip_tags( mysql_real_escape_string( $_POST['password'] ) );
$password2 = strip_tags( mysql_real_escape_string( $_POST['password2'] ) );
$passwordmd5 = md5( $password );
$email = strip_tags( mysql_real_escape_string( $_POST['email'] ) );
$ip = $_SERVER['REMOTE_ADDR'];
if ( empty( $username ) || empty( $password ) || empty( $password2 ) || empty( $email ) )
{
echo "All fields required.";
}
else
{
$userQ = mysql_query( "SELECT * FROM users WHERE `username` = '{$username}'" );
$emailQ = mysql_query( "SELECT * FROM users WHERE `email` = '{$email}'" );
if ( mysql_num_rows( $userQ ) == 1 )
{
echo "This user already exists.";
}
else
{
if ( mysql_num_rows( $emailQ ) == 1 )
{
echo "Someone has already used this email.";
}
else
{
if ( $password !== $password2 )
{
echo "The passwords do not match.";
}
else
{
mysql_query( "INSERT INTO `users` (`username`, `password`, `email`, `ip`) VALUES ('{$username}', '{$passwordmd5}', '{$email}', '{$ip}')" );
echo "You have successfully registered";
}
}
}
}
}
else
{
?>
<form method="post">
Username: <input type="text" name="username" maxlength="25" /><br />
Password: <input type="password" name="password" maxlength="20" /><br />
Password again: <input type="password" name="password2" maxlength="20" /><br />
Email: <input type="text" name="email" maxlength="255" /><br />
<input type="submit" name="register" value="Register" />
</form>
<?php
}
}
?>
logout.php
PHP:
<?php
include( "db.php" );
if ( isset( $_SESSION['loggedin'] ) == "1" )
{
session_destroy();
header( "Location: index.php" );
}
else
{
echo "You are not logged in.";
}
?>
Enjoy,
- m0nsta.