Publishing System Settings Logout Login Register
Managing Users with PHP Sessions and MySQL part2
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on April 17th, 2010
1133 views
PHP Coding
The Signup Script

If you're used to writing database-driven Web sites, then the signupscript should seem pretty straightforward. First we need to write acouple of snippets of code that will perform common functions likeconnecting to the database that will store the usernames and passwordsfor the site. These snippets will take the form of PHP functions storedin include files. We'll use one include file to house database-relatedfunctions (db.php), and another to store more generalfunctions (common.php).

First, here's the code for db.php:

<?php // db.php �

$dbhost = "localhost"; �
$dbuser = "user"; �
$dbpass = "password"; �

function dbConnect($db="") { �
global $dbhost, $dbuser, $dbpass; �

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) �
or die("The site database appears to be down."); �

if ($db!="" and !@mysql_select_db($db)) �
die("The site database is unavailable."); �

return $dbcnx; �
} �
?>

<SCRIPT language='JavaScript1.1'SRC="http://ad.doubleclick.net/adj/N4270.SitePoint/B4315639.30;abr=!ie;sz=300x250;ord=1270982822.82?"> </SCRIPT> <NOSCRIPT> <ahref="http://adscluster.aws.sitepoint.com/openx/www/delivery/ck.php?oaparams=2__bannerid=268__zoneid=6__cb=ba4d97113c__oadest=http%3A%2F%2Fad.doubleclick.net%2Fjump%2FN4270.SitePoint%2FB4315639.30%3Babr%3D%21ie4%3Babr%3D%21ie5%3Bsz%3D300x250%3Bord%3D%7Btimestamp%7D%3F"target="_top"> <IMGSRC="http://ad.doubleclick.net/ad/N4270.SitePoint/B4315639.30;abr=!ie4;abr=!ie5;sz=300x250;ord=1270982822.82?"BORDER=0 WIDTH=300 HEIGHT=250 ALT="Click Here"></A> </NOSCRIPT>

The dbConnect function defined here can be called withor without an argument, because we've assigned a default value ("")to the $db argument in the function declaration. Thefunction begins by connecting to the MySQL server using the $dbhost,$dbuser, and $dbpass variables at the top ofthe file (you'll need to set these to appropriate values foryour server), and then if a database name was given it selectsthat database. Assuming everything proceeds without error, the referenceto the database connection is returned.

The second include file, common.php, also contains asingle function:

<?php // common.php �

function error($msg) { �
?> �







<? �
exit; �
} �
?>

We'll use this error function to tell the user when he or she hasdone something wrong. It takes an error message as an argument, and thendisplays it in a JavaScript pop-up message before backing up to theprevious page. This function ends the script that calls it with the exit command, so it's suitable for use when something goes wrong in one ofyour scripts.

With these boring details out of the way, you can now turn yourattention to the signup script (signup.php). The scriptbegins by loading the two include files we just wrote:

<?php // signup.php �
include 'common.php'; �
include 'db.php';

This assumes that the files in question are available in the includepath. Consider, for example, the following include path, which I use inmy php.ini file:

include_path = ".;c:\php4\pear;d:\www\phpinclude"

On a Unix server, it might look like this:

include_path =".:/usr/local/php/lib/php:/home/kyank/phpinclude"

In either case, you can choose to put your include files in the samedirectory as the file(s) that use them, or place them in a directorylisted in the include path. The latter choice is a safer for filescontaining sensitive information like passwords, because if the PHPsupport in your Web server ever fails, the information in PHP files notstored below your server's Web root directory will not be exposed toprying eyes.

Next, the script checks for the presence of a $_POST['submitok'] variable, which would indicate that the signup form had been submitted.If the variable is not found, the script displays the form from theprevious section for the user to fill in:

if (!isset($_POST['submitok'])): �
// Display the user signup form �
?> �

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> �


New User Registration �

Next Page
Pages: 1 2
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
adana

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top