Creating a register and login page, by creating a mysql database and all php files.
I was reading a thread that is dead now, so i started this one:

Simple PHP login:

Create a database (mysqladmin)

Name the table "dbUsers." It will need 4 fields:

Name Type Addition
id int(10) Primary Key, AUTO_INCREMENT
username varchar(16) Unique
password char(16)
email varchar(25)

Create a new file and name it dbConfig.php This will file will connect to the database

<? 
// Replace the variable values below 
// with your specific database information. 
$host = "localhost"; 
$user = "UserName"; 
$pass = "Password"; 
$db = "dbName"; 
// This part sets up the connection to the 
// database (so you don't need to reopen the connection 
// again on the same page). 
$ms = mysql_pconnect($host, $user, $pass); 
if ( !$ms ) 
{ 
echo "Error connecting to database.\n"; 
} 
// Then you need to make sure the database you want 
// is selected. 
mysql_select_db($db); 
?>

Registration name this file "register.php"

<?php
// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.php");
//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
 {
 $bInputFlag = false;
 foreach ( $_POST as $field )
  {
  if ($field == "")
   {
   $bInputFlag = false;
   }
  else
   {
   $bInputFlag = true;
   }
  }
 // If we had problems with the input, exit with error
 if ($bInputFlag == false)
  {
  die( "Problem with your registration info. "
   ."Please go back and try again.");
  }
 // Fields are clear, add user to database
 //  Setup query
 $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
  ."VALUES ('".$_POST["username"]."', "
  ."PASSWORD('".$_POST["password"]."'), "
  ."'".$_POST["email"]."')";
 //  Run query
 $r = mysql_query($q);

 // Make sure query inserted user successfully
 if ( !mysql_insert_id() )
  {
  die("Error: User not added to database.");
  }
 else
  {
  // Redirect to thank you page.
  Header("Location: register.php?op=thanks");
  }
 } // end if
//The thank you page
elseif ( $_GET["op"] == "thanks" )
 {
 echo "<h2>Thanks for registering!</h2>";
 }

//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\">\n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\">\n";
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\">\n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\">\n";
 echo "<input type=\"submit\">\n";
 echo "</form>\n";
 }
// EOF
?>

Login name this file "login.php"

`<?php
session_start();
// dBase file
include "dbConfig.php";
if ($_GET["op"] == "login")
 {
 if (!$_POST["username"] || !$_POST["password"])
  {
  die("You need to provide a username and password.");
  }

 // Create query
 $q = "SELECT * FROM `dbUsers` "
  ."WHERE `username`='".$_POST["username"]."' "
  ."AND `password`=PASSWORD('".$_POST["password"]."') "
  ."LIMIT 1";
 // Run query
 $r = mysql_query($q);
 if ( $obj = @mysql_fetch_object($r) )
  {
  // Login good, create session variables
  $_SESSION["valid_id"] = $obj->id;
  $_SESSION["valid_user"] = $_POST["username"];
  $_SESSION["valid_time"] = time();
  // Redirect to member page
  Header("Location: members.php");
  }
 else
  {
  // Login not successful
  die("Sorry, could not log you in. Wrong login information.");
  }
 }
else
 {
//If all went right the Web form appears and users can log in
 echo "<form action=\"?op=login\" method=\"POST\">";
 echo "Username: <input name=\"username\" size=\"15\">";
 echo "Password: <input type=\"password\" name=\"password\" size=\"8\">";
 echo "<input type=\"submit\" value=\"Login\">";
 echo "</form>";
 }
?>`

Members Area name this file "members.php", and include on pages that are only for registered users

`<?php
session_start();
if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
Header("Location: login.php");
}
// Member only content
// ...
// ...
// ...
// Display Member information
echo "<p>User ID: " . $_SESSION["valid_id"];
echo "<p>Username: " . $_SESSION["valid_user"];
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
// Display logout link
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
?>`

logout name this file "logout.php"

`<?php
session_start();
session_unset();
session_destroy();
// Logged out, return home.
Header("Location: index.php");
?>`

(credits: macneato)

The problem is, that im having trouble to get it working..

So:
-i created all the php files.
-edited them all (i guess).
-uploaded them all to server.

When i try to register, i get the "Error: User not added to database." message..

Any help would be much apreciated.
(im kinda noob on this)

Thanks,

Ricardo Jorge

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

@ricardojorge.net

The problem is, that im having trouble to get it working..

I don't feel you have a general idea how this form works.

Read this so you can see how it works:

http://www.html-form-guide.com/php-form/php-registration-form.html

When i try to register, i get the "Error: User not added to database." message..

It means that you can't connect to the database correctly because there's something wrong with your query.

Any help would be much apreciated. (im kinda noob on this)

You need to understand how the database works.

@ricardojorge
register.php line 35:
$r = mysql_query($q);
add some error checking:
if(!$r = mysql_query($q)) die(mysql_error());
do you get an error message?

@lastmitch I agree with you, but I fancied the challenge and was already looking :)

sory for the noobie, but by now, almost all i can do is [copy > paste], and then see the changes and learn with them..

The problem persists...

i´v created the database as it was in the original post, but istill cant get it..
Any (noob) tips? :)

thanks

iv changed line 35, now i get:

No database selected...

what am i doing wrong!?

dbConfig.php

<?
// Replace the variable values below
// with your specific database information.
$host = "fdb3.atspace.com";
$user = "*****_fusion";
$pass = "*******";
$db   = "dbUsers";
// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want
// is selected.
mysql_select_db($db);
?>

Your script is not selecting the database before it tries to add the new user.
This line:
mysql_select_db($db);
...needs error checking (notice a pattern here?)
if(!mysql_select_db($db)) die(mysql_error());

Please correct the username and password.. in dbConnect.php
It should be the username or 'server's owner' by default, which generally is 'root' and password being blank.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.