not completed yet

PART 1

Lets create the table structure we need to register our users.


CREATE TABLE members (
member_id int(10) unsigned NOT NULL auto_increment,
first_name varchar(100) NOT NULL,
last_name varchar(100) NOT NULL,
email_address varchar(100) NOT NULL,
date_registered datetime NOT NULL,
last_accessed datetime NOT NULL,
password char(32) NOT NULL,
profile_name varchar(30) NOT NULL,
PRIMARY KEY (member_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;



copy and paste the sql into phpMysqladmin sql window and execute it. You would create a table members in your database.


PART 2

copy and paste the following code into a php file called register.php. If anybody wants to register with your site they have to access through the site.

http://www.yourserver.com/register.php




if($_POST['submit_registration']) {

// enter your database info here -----

$db_name = ; // find your db name and add it over here
$user = ; // find your db username and add it here
$password = ; // add your password to the db here


// check user input and filter them
$firstname = mysql_real_escape_string($_POST['fname']);
$lastname = mysql_real_escape_string($_POST['lname']);
$profile = mysql_real_escape_string($_POST['pname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);

if(!$firstname || !$lastname || !$profile || !$email || !$password || !$cpassword) {

echo "All fields are required, please fill them";
}
else {

// ------------------- DON'T TOUCH BELOW NOW-----------------
$db_link = @mysql_connect("localhost",$user,$password);

if($db_link)
@mysql_select_db($db_name);
else {
echo "Error in your db connection, check db_name, user_name and password";
exit;
}

if(!mysql_query("select member_id from members where email='$email' or profile_name='$profile')) {
$has_success = @mysql_query("insert into members values(null,'$firstname','$lastname','$email',now(),now(),md5($password),'$profile')");
if($has_success)
echo "Your registration was successful";
}
else
echo "The profile name / email is already registered");
}
else {
// display registration form
?>




<form method="post">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td>First Name</td>
<td></td>
<td><input name="fname" maxlength="255" size="20" value="" type="text"></td>
</tr>
<tr>
<td>Last Name</td>
<td></td>
<td><input name="lname" maxlength="255" size="20" value="" type="text"></td>
</tr>
<tr>
<td>Email</td>
<td></td>
<td><input name="email" maxlength="255" size="20" value="" type="text"></td>
</tr>
<tr>
<td>Profile Name</td>
<td></td>
<td><input name="pname" maxlength="255" size="20" value="" type="text"></td>
</tr>
<tr>
<td>Password</td>
<td></td>
<td><input name="password" maxlength="255" size="20" value="" type="text"></td>
</tr>
<tr>
<td>Confirm Password</td>
<td></td>
<td><input name="cpassword" maxlength="255" size="20" value="" type="text"></td>
</tr>
</tbody></table>
</form>





}

Registration and Login Script in PHP



CREATE TABLE members (
  member_id int(10) unsigned NOT NULL auto_increment,
  first_name varchar(100) NOT NULL,
  last_name varchar(100) NOT NULL,
  email_address varchar(100) NOT NULL,
  date_registered datetime NOT NULL,
  last_accessed datetime NOT NULL,
  password char(32) NOT NULL,
  profile_name varchar(30) NOT NULL,
  PRIMARY KEY  (member_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

PHP Introduction

1.Hello World example

Use any of your favourite text editor and type your first php code and save it with “helloWorld.php”. Upload it to your web server and then execute it.


echo “Hello World”;


to execute : http://www.yourserver.com/helloWorld.php ( place it in the root directory )

semicolon means end of a line.


2.Conditional Check if..else

check for a condition to be true or false. Why do you need to check for conditions? Take a scenario like this. If you are checking the site in the morning I should say "Good morning", so how do we handle this now.


// if time is less than 12'o clock the site would display
// "Good Morning"
if($time < 12)
echo "Good Morning";


How do we say good evening now?

if($time < 12)
echo "Good Morning";
else // time is more than 12
echo "Good Evening";






Where are those curly braces? If you use one statement just after the if ..condition then you don't need it but if you have more than one line then you shuld open and close curly braces.


eg:
if($time < 12) {
echo "Good Morning";
$num_of_visits = $num_of_visits + 1;
}




3.Looping

Loop is used to repeat a job as many time you want

while loop


$i=1;
while($i<5) {
echo “Hello World”.$i;
}


The above code snippet would display
Hello World 1Hello World 2Hello World 3Hello World 4


for…loop


for($i=0; $i<5; $i++)
echo $i;



The loop will display 01234 as output.



do…while

some times you need your loop to execute at least once regardless of condition


$x = 5;
do {
echo “Hello World”;
}while($x<5);


though x=5 initially, the loop would execute once because the conditional statement is at the end. When it reaches the end the condition x<5 returns false the loop will not execute any more.


// to validate rgb input

static function is_valid_rgb($rgb_values) {
// format expected is: array(255,125,60)
if(!is_array($rgb_values) || count($rgb_values) != 3)
return false;

foreach($rgb_values as $rgb_value) {
if(!preg_match('/^[0-9]{1}$|^[0-9]{2}$|^[0-1]{1}[0-9]{1}[0-9]{1}$|^[2]{1}[0-5]{1}[0-5]{1} $/',$rgb_value))
return false;
}
return true;

} // end of function




// to validate cymk input

static function is_valid_cymk($cymk_values) {

// format expected is: array(255,125,60,199)
if(!is_array($cymk_values) || count($cymk_values) != 4)
return false;

foreach($cymk_values as $cymk_value) {
if(!preg_match('/^[0-9]{1}$|^[0-9]{2}$|^[0-1]{1}[0-9]{1}[0-9]{1}$|^[2]{1}[0-5]{1}[0-5]{1}$/',$cymk_value))
return false;
}

return true;
}



Some Links (off programming)