View unanswered posts | View active topics It is currently Mon Dec 18, 2017 4:17 am



Reply to topic  [ 1 post ] 
 Register & Login W/ SCRIPTS! And db included! 
Author Message
Junior Member

Joined: Sun Sep 13, 2009 4:08 pm
Posts: 54
Location: United States Of America
Post Register & Login W/ SCRIPTS! And db included!
Ok, this will be a HUGE tutorial, but I am doing it, lets start.
Our first script is creating our database, do not change anything here except your Database connection.
Put this in some random file, refresh the page, and you should have the database setup.
[PHP]<?php
$con = mysql_connect("","root","root"); //Change your info <<
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE Users",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

// Now we create the table
mysql_select_db("Users", $con);
$sql = "CREATE TABLE Users
(
id int,
Username varchar(20), //You can set these lengths to how you want them
Password varchar(20),
FirstName varchar(20),
LastName varchar(20),
)";
//Id should be set to auto increment, so once you enter your database, go inside, and change
// the id to Primary Key, and auto increment. Thanks.

// Ok, now we have our simple Database created, now on to another script.

// Execute query
mysql_query($sql,$con);
[/PHP]Ok, the next script is were you are going to register.

Post.php is the file name :) Remember, this is the register page.
[HTML] <form action='Received.php' method='Post'>
Username: </br> <input type='Text' name='Username'> </br>
Password: </br> <input type='password' name='Password'> </br>
Firstname: </br> <input type='text' name='Firstname'>
Lastname: </br> <input type='text' name='Lastname'>
</br><input type='submit' value='Register'>
</form>[/HTML]Ok, now we are going to have to access the database so we can tell that the information has been recieved. So the next script is:
Recieved.php

[PHP] <?php
$con = mysql_connect("","root","root"); //remem to change

mysql_select_db("Users",$con);

$sql = "INSERT INTO Users (Username, Password, Firstname, Lastname)
VALUES
('$_POST[Username]','$_POST[Password]','$_POST[Firstname]','$_POST[Lastname]')";

if (!mysql_query($sql,$con))
{
die('Could not register');
}
echo "You have successfully registered!";

?>

[/PHP]By the way, I made these scripts, so if there is any errors, just contact me somehow.

Alright, next is the login script. This is quite a big one:

login.php NOTE: This is the script, not the fields!
[PHP]<?php

session_start();

$Username = $_POST['Username'];
$Password = $_POST['Password'];

if ($Username&&$Password)
{

$connect = mysql_connect("","root","root") or die("Couldn't connect to database");
mysql_select_db("Users") or die("Couldn't find Database");

$sql = "INSERT INTO Users (Username, Password, Firstname, Lastname)
VALUES
('$_POST[Username]','$_POST[Password]','$_POST[Firstname]','$_POST[Lastname]')";

$numrows = mysql_num_rows($query);

if ($numrows!=0)
{

while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
}

$sql = "INSERT INTO Users (Title, Name, Body)
VALUES
('$_POST[Title]','$_POST[Name]','$_POST[Body]')";

// check to see if they match
if ($username==$dbusername&&$password==$dbpassword)
{
echo "Welcome, $Username! <a href='member.php'>Click</a> here to enter the member page.";
$_SESSION['Username']=$Username;
}
else
echo "Incorrect password!";

}
else
die("That user doesn't exist!");


}
else
die("Please enter a username and a password.");



?>[/PHP]Alright, now next is the logout.

logout.php

[PHP]<?php

session_start();

session_destroy();

echo "You've been logged out. <a href='index.php'>Click</a> here to return.";


?>[/PHP]Now the member page:

member.php
[PHP]<?php

session_start();

if ($_SESSION['Username'])
echo "Welcome, ".$_SESSION['Username']."!
<a href='logout.php'>Logout</a>";
else
die("You must be logged in!");
?>[/PHP]Next, is the login fields:

index.php
[HTML]<html>

<form action='login.php' method='post'>
<table>
<tr>
<td>
Username:
</td>
<td>
<input type='text' name='Username'>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type='password' name='Password'>
</td>
</tr>
</br>
</table>
<input type='submit' value='login'></br>
</form>

</html>[/HTML]Alright, thats about it! Thanks for looking! - Spencer Chase. Feel free to ask for more tutorials!


Thu Dec 31, 2009 12:32 pm
Report this post
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.