Archive for category PHP
PHP Login System Tutorial – Part 3
This will be the last part of the tutorial. Below are links to the previous 2 parts. Sorry for the lack of whitespace use, I’m trying to save you from scrolling horizontally ![]()
Link to part 1
Link to part 2
So far, we can register and log in. Now we want to be able to update our profile and add pictures. We’ll need to add another table in our database to store references of the users pictures.
If we were to store the actual picture into the database it could become very big, especially if you decide to allow your users to upload videos too. A better option would be to upload the picture into a directory on the web server and keep a reference to the picture in our database.
So first lets add our table. I added size and type to explain how to retrieve those values but I won’t be using them in my version of the site.
CREATE TABLE `db`.`user_photos` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `profile_id` INT NOT NULL , `title` VARCHAR( 128 ) NOT NULL , `size` INT NOT NULL , `type` VARCHAR( 128 ) NOT NULL , `reference` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM
The first 2 parts of the login system set everything up but the output looks terrible. We’ll add some css to make the site somewhat presentable. Give the sections a background and align the form elements with css.
style.css
.formElm { margin-bottom:5px; } .formElm label { float:left; width:120px; } div.divider { width:450px; margin:20px auto; background-color:#C1f0f6; border:4px #0ebfe9 double; padding:10px; }
Now we’re ready to make our profile control panel. At the top of our profilecp.php we need to start our session to see if the user is authenticated. So we start our session and include our database info.
Read the rest of this entry »
PHP Login System Tutorial – Part 2
So far we’ve made our database, connected to it, and wrote a function to get rid of
SQL Injection attempts. Now we can make the registration page.
We’ll have a form with a couple fields and a submit button that sends everything to our database.
The first thing we’re going to do is include the db_connect.php page. This will connect us to our database and give us access to the protect function we created earlier.
This form will then submit to itself and insert the data into our db.
< ?php //Create registration form (register.php) include "db_connect.php"; if(!$_POST['submit']) // 'submit' hasn't been clicked so output html. { ?>
So what the first IF statement does is checks to see if the form has been submitted. If it hasn’t it outputs the html form.
<html> <form method="post" action="register.php"> First Name: <input type="text" name="first"/> Last Name: <input type="text" name="last"/> Desired Username: <input type="text" name="username"/> Password: <input type="password" name="password"/> Confirm Password: <input type="password" name="pass_conf"/> Email: <input type="text" name="email"/> About: <textarea name="about">Tell us about yourself</textarea> <input type="submit" name="submit" value="Register"/> </form> or <a href="index.php">Login</a> </html>
Notice the forms action is register.php which is the page itself. When ’submit’ is posted we go to the else block here. This is where we store the posted values into variables. We call our protect function on these values to cleanse them of any injection attempts.
PHP Login System Tutorial – Part 1
This is Part 1 of my PHP Login System tutorial. By the end of this series, you’ll have your own custom social networking site. Users will be able to register, login, edit their profile and add pictures. We’re aiming for a very stripped down version of Facebook and Myspace.
For this tutorial you’ll need a webserver running PHP and mySQL. Click here for instructions on how to set one up on your local machine with XAMPP.
Lets start by making our database. Our users table will have 7 fields.
id, first, last, username, password, email and about.
Heres the SQL code to make this happen. You can paste it into your phpmyadmin SQL tab. Make sure you change the `db` in the first line to match the name of your database.
CREATE TABLE `db`.`users` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `first` VARCHAR( 32 ) NOT NULL , `last` VARCHAR( 32 ) NOT NULL , `username` VARCHAR(32) NOT NULL, `password` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `about` TEXT NOT NULL ) ENGINE = MYISAM;
We make the id auto_increment so that every user that is added to the db has their own id. Since this field will be unique, we’ll make it our primary key. It will be used to reference each user later.
Our database is setup. We just need to connect to it using php. We’ll create our first php file, “db_connect.php”.
PHP Basics
Heres a quick intro to PHP. A lot will be left out, I’m assuming you know basic programming (variables and loops). W3 Schools is a great place to learn PHP more in depth.
To try this code make sure you have XAMPP installed, check out this tutorial if you haven’t set it up yet.
Every script should begin and end with the php open/close tags.
< ?php //Comments //rest of code here ?>
Here are some examples
< ?php // two backslashes are used for comments // A variable is created using the dollar sign before the name of the var $v1 = "bananas" ; //semicolon after each statement //echo is used to print to the screen echo $v1; //You can print multiple items using a comma $v2 = "apples" ; echo $v1, $v2; //If statements are similar to those in C++ and Java. $uno = 1 ; $dos = 2 ; if($uno < $dos) { echo "WOO!" ; } else { echo "WTF!?"; } ?>


Recent Comments