Show DevBest [PHP] [REL] Very simple register & login script

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
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
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.
 

Gajeel

Well-Known Member
Oct 4, 2011
2,411
413
Thanks for sharing.
I made a sample site for your script, hibzy.webege.com.

I'd like to ask if you already have projects with the profiles/view profiles of others/birthdate/profile pic/wall post etc...

Thanks! :)
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Yes I do,
 

Izz

New Member
Nov 5, 2011
6
0
Very nice and easy script, I used your scripts to customize and learn more PHP. I can say, you've done a good job.
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,195
3,906
Another great contribution, nice, can I ask whats the link to your YouTube channel? I'll subscribe :p.

Didn't know you could use , or what the difference is of using it? Or is there none?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I've been using MySQL for years! Lmfao -- check out my projects if you haven't already, a lot of them use MySQL databases -

And my channel is: :p
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,195
3,906
I've been using MySQL for years! Lmfao -- check out my projects if you haven't already, a lot of them use MySQL databases -

And my channel is: :p

Ah, I didn't mean it like that the forums must of taken if off my post, so let me restate that:

PHP:
@mysql

I meant that, xD.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Oh, to stop errors being shown you mean? :p
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
Is there some sort of code i can put in all of my member-only pages so that it checks to see if the user is logged in, and if they're not then send them to the login/register page?
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Is there some sort of code i can put in all of my member-only pages so that it checks to see if the user is logged in, and if they're not then send them to the login/register page?

Try something like this -
PHP:
if($_SESSION['user_id'] == NULL) {
    $_SESSION['login_error'] = "You need to login to view this page!";
    die(header('Location: '.$site.'/')); }
Obviously your gonna have to change a few parts of the above script
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Very nice and easy script, I used your scripts to customize and learn more PHP. I can say, you've done a good job.
Thanks and you're welcome.

Also, I do indent my code - it's just that for some reason when I copied and pasted it in to DevBest and posted the thread, it un-indented it for some reason.
 

At0m

Active Member
Sep 10, 2011
101
40
Other nice script from
Thanks for sharing man, ;)
 
Status
Not open for further replies.

Users who are viewing this thread

Top