In this section we will begin setting up our database, as well as connecting to our database, and finally we will begin creating our User class to store all of our …
Original source
In this section we will begin setting up our database, as well as connecting to our database, and finally we will begin creating our User class to store all of our …
Original source
27 responses to “Login & Registration tutorial PHP and Mysql”
Mate are you fucking joking? Thanks for making this video and all, I'm sure the code works fine, but you can't just make a video, say 'see you in part 2' and then not have a part 2…
I, as a novice php programmer just took 1 hour out of my day to follow this tutorial, and now have almost 200 lines of code which mean absolutely NOTHING to me…
I understand, would be great if you could. Im trying to master the login and reg using OOP style programming. I found the first video very helpful. I had to tweak it a bit to make it work but it is very directional.
Thanks for your response
Regards
Dave
brandon…wheres part 2?
WELCOME!
if you are looking into comments for code
<?php
//User class to store user information for logged user
class User{
private $uid; // User id
private $fields; // other records on file
//initialize our user object
public function __construct(){
$this->uid = null;
$this->fields = array('username' => '', 'password' => '', 'emailAddr' => '', 'isActive' => false);
}
//overide magic method to retrive properties
public function __get($field){
if($field == 'uesrID'){
return $this->uid;
}else{
return $this->fields[$field];
}
}
//Overide magic method to set properties
public function __set($field, $value){
if(array_key_exists($field, $this->fields)){
$this->fields[$field] = $value;
}
}
//return if username is valid format
public static function validateUsername($username){
return preg_match('/^[A-Z0-9]{2,20}$/i', $username);
}
public static function validateEmailAddr($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
//return an object populated based on a users records
public static function getById($user_id){
$user = new User();
$query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USER_ID = %d', DB_TBL_PREFIX, $user_id);
$result = mysql_query($query, $GLOBALS['DB']);
if(mysql_num_rows($result)){
$row = mysql_fetch_assoc($result);
$user->username = $row['USERNAME'];
$user->password = $row['PASSWORD'];
$user->emailAddr = $row['EMAIL_ADDR'];
$user->isActive = $row['IS_ACTIVE'];
$user->uid = $user_id;
}
mysql_free_result($result);
return $user;
}
//return an object populated based on a username
public static function getByUsername($username){
$user = new User();
$query = sprintf('SELECT USER_ID, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USERNAME = "%s"', DB_TBL_PREFIX, mysql_real_escape_string($username, $GLOBALS['DB']));
$result = mysql_query($query, $GLOBALS['DB']);
if(mysql_num_rows($result)){
$row = mysql_fetch_assoc($result);
$user->username = $username;
$user->password = $row['PASSWORD'];
$user->emailAddr = $row['EMAIL_ADDR'];
$user->isActive = $row['IS_ACTIVE'];
$user->uid = $row['USER_ID'];
}
mysql_free_result($result);
return $user;
}
//save the record to the database
public function save(){
if($this->uid){
$query = sprintf('UPDATE %sUSER SET USERNAME = "%s", PASSWORD = "%s", EMAIL_ADDR = "%s", IS_ACTIVE = %d WHERE USER_ID = %d', DB_TBL_PREFIX,
mysql_real_escape_string($this->username, $GLOBALS['DB']),
mysql_real_escape_string($this->password, $GLOBALS['DB']),
mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
$this->isActive, $this->userId);
return mysql_query($query, $GLOBALS['DB']);
}
else{
//if the user has not registered yet
$query = sprintf('INSERT INTO %sUSER (USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE) VALUES ("%s", "%s", "%s", %d)', DB_TBL_PREFIX,
mysql_real_escape_string($this->username, $GLOBALS['DB']),
mysql_real_escape_string($this->password, $GLOBALS['DB']),
mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
$this->isActive);
if(mysql_query($query, $GLOBALS['DB'])){
$this->uid = mysql_insert_id($GLOBALS['DB']);
return true;
}else{
return false;
}
}
}
//set record and return token
public function setInactive(){
$this->isActive = false;
$this->save(); // Make sure the record is saved
$token = random_text(5);
$query = sprintf('INSERT INTO %sPENDING (USER_ID, TOKEN) VALUES (%d, "%s")', DB_TBL_PREFIX, $this->uid, $token);
return (mysql_query($query, $GLOBALS['DB'])) ? $token : false;
}
//Clear the users pending status and set the record as active
public function setActive($token){
$query = sprintf('SELECT TOKEN FROM %sPENDING WHERE USER_ID = %d AND TOKEN = "%s"', DB_TBL_PREFIX, $this->uid, mysql_real_escape_string($token, $GLOBALS['DB']));
$result = mysql_query($query, $GLOBALS['DB']);
if(!mysql_num_rows($result)){
mysql_free_result($result);
return false;
}else {
mysql_free_result($result);
$query = sprintf('DELETE FROM %PENDING WHERE UER_ID = %d AND TOKEN = "%s"', DB_TBL_PREFIX, $this->uid, mysql_real_escape_string($token, $GLOBALS['DB']));
if(!mysql_query($query, $GLOBALS['DB'])){
return false;
}else{
$this->isActive = true;
return $this->save();
}
}
}
}
?>
Anyone get this error: FATAL ERROR Cannot redeclare User::__get() on line number 26 in User.php?? Any info would be appreciated
This is great pls make one more video.
you should remove this if there is no part 2 :@
PLEASE MAKE PART 2
die('PLEASE');
Your tutorials are so easy to follow!
Many people have had issues with this video, to solve this issue I'll be making a video tomorrow about a better login ®istration I apologize for any inconvenience.
Hello Brandon that for the tutorial but I just have one problem when I go in the SQL code area of the local host and input the code that you gave to us on you tube I get a error that says this MySQL sever version for the right syntax to use near 'CREATE TABLE KV_PENDING(USER_ID INTEGER UNSIGNED NOT NULL, TOKEN CHAR(10) N' at line 14. Brandon I'm using Xampp v3.2.1 so I think I'll need to find the right syntax to put near CREATE TABLE KV_PENDING etc…. Can you help me find the right state there.
Thanks Tony D.
remove this garbage
gonna create a new one and it takes you 8 months e.e
God bro when u going to make a new 1 i waited 8 months
Excuse but your explanations are horrible!
a blank page only ?
im done with it . and there's no error . what is next? there's no layout ? T_T
Can I have source in my email address plz
Cool, no errors. Sooo.. how can we incorporate this into a page? Can we get a quick demo with a user registration form?
I agree with this, because I am getting errors and when comparing my code to the code in this video it's 100% the same.
second vid?????
Please mane the second part 😉
Fantastic video and a great introduction to object orientated programming. I do have a few questions though…excuse me for being a noob! Basically does it matter what version of php you are running or its compiler, as I keep getting a error for all the the lines that start with public function __ for example: public function __set it really doesn't like and comes up with this error: "Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:xampphtdocsocsopsuser.php on line 35".
Thanls for the vid, im getting an Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:xampphtdocsUser.php on line 33, at line 33 in my code its : //return if username if valid format
public static function validateUsername($username){
return preg_match('/^[A-Z0-9]{2,20}$/i', $username);
}
Any suggestions?????
Great tutorial! Really looking forward to the second video. Hope it comes up soon. Thanks!
Hey, awesome tut, could you send me a download link to all the codes please? Much appreciated 🙂
Will be creating the next tutorial within a weeks time. Just been pretty busy lately, Mic is super sensitive so It has to be extremely quiet in my house lol. I apologize for the wait.
Cool vid! Thanks. Waiting for the next video