Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,503 members, 7,954,924 topics. Date: Saturday, 21 September 2024 at 12:35 PM

Help On Login Code For Php N How To Store On Data Base. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On Login Code For Php N How To Store On Data Base. (1301 Views)

I Need Help On How To Store Records On J2me App Using Rms. / I Need A Help To Write A Simple Login Form Using Vb.net / [Code Request] Code for PHP Login And Signup (2) (3) (4)

(1) (Reply) (Go Down)

Help On Login Code For Php N How To Store On Data Base. by callydon(m): 9:31am On Oct 12, 2013
Hi, gurus in d house.
plz I need help on how to put a login code on my php website...
I also need help on how to make d details submitted during registration by users to be stored in my data base...
NB: Am using Dream Weaver in d design n am not good with codes.
Thanks in advance.
Re: Help On Login Code For Php N How To Store On Data Base. by CODEEATER(m): 9:38am On Oct 12, 2013
callydon: Hi, gurus in d house.
plz I need help on how to put a login code on my php website...
I also need help on how to make d details submitted during registration by users to be stored in my data base...
NB: Am using Dream Weaver in d design n am not good with codes.
Thanks in advance.
Dreamweaver de spoil person...makes u lazy...use notepad++ n write code,u'l feel a lot beta...as 4 d login ish, Hw grounded are you in php?
Re: Help On Login Code For Php N How To Store On Data Base. by nollyj: 11:16am On Oct 12, 2013
I wrote this code now based on your request. Please not that it is not tested but if you run into problem just paste it here. I will try as much as possible to help.

I made two simple classes in Php. One is for database connection and the other to query database.

<?php
include_once 'blog/config.php';

class DbConnect{


private $connect;


public function __construct(){

$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (mysqli_connect_errno($this->connect))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}

public function getDb(){

return $this->connect;
}

}

?>




Second Class to query database user table

<?php

include_once 'db.php';


class DatabaseLayer{

private $tables;

public function __constructor(){

$this->tables = "User_table";
}


public function createAccount($username, $password, $email){

// database clase
$com = new DbConnect();

$notiify = "";

// filter inputs
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);


$error_array = array();

// check varliable after sanitazation
if(!$username){
$error_array[] = "Invalid Username";
}

if(!$password){
$error_array[] = "Invalid Password";
}

if(!$email){
$error_array[] = "Invalid Email";
}

// check if username or email has been used by another user
$is_used = $this->checkEmailUsernameExist($username, $email);

if(!$is_used){

$error_array[] = "Username and or email address has been taken. Please change to a new one";
}


if(!is_null($error_array)){

// encrypt your password with whatever method you like . I use md5 without salt

$password = md5($password);

// insert into the user table
$sql = "INSERT INTO " . $this->tables . " (username, password, email) VALUES ( '$username', '$password', '$email')";

$inserted = mysqli_query($com->getDb(), $sql);

if($inserted == 1){

$notiify = "Your Registration is successful";
}
else{

$notiify = "Your Registration failed. Please contact the admin";
}
}
else{

//check number of errors
$error_size = count($error_array);

for($i = 0; $i < $error_size; $i++){

$notiify .= $error_size[$i] . "\n";
}
}

// close database connection
if(!is_null($com)){

mysqli_close($com->getDb());
}

return $notiify;
}

// check if username or password has been used before
private function checkEmailUsernameExist($username, $email){

$com = new DbConnect();

$is_email_username_exist = false;

$sql = "SELECT * FROM " . $this->tables . " where username = '$username' OR password = '$password";
$result = mysqli_query($com->getDb(), $sql);

if(mysqli_num_rows($result) > 0){

$is_email_username_exist = true;
}

// close database connection
if(!is_null($com)){

mysqli_close($com->getDb());
}

return $is_email_username_exist;
}

?>


How to use this code.

1. Create your user_table in your database with fields username, password, email and others

2. Add your database connection details in a config.php file as a Constants using the following constant names - DB_HOST, DB_USER, DB_PASSWORD, DB_NAME


3 When a user submit the registration form. Then get the values in the post or get array variable like this


 

if(isset($_POST['submit'])){

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];


// include the com.php file
include_once 'com.php'

// create an object of the DatabaseLayer class
$db_layer = new DatabaseLayer();

// call the create new user method

$db_layer->createAccount($username, $password, $email)
}




Please see if you can understand the code and try to fix any error before asking more questions. It will help in your learning process.
Re: Help On Login Code For Php N How To Store On Data Base. by CODEEATER(m): 1:29pm On Oct 12, 2013
D OP is obviously a newbie, cnt code a login system with normal procedural code,na OOP e wan understand?

U 4 wait make e answer my first ? First..


Anyway...llhope it works
Re: Help On Login Code For Php N How To Store On Data Base. by veltany: 10:37pm On Oct 12, 2013
Create a file called login.php and insert the code
require_once 'db.php';

$page_mode = isset($_POST['page_mode']) ? $_POST['page_mode'] : '';

$error_string = '';

if ($page_mode == 'login')
{
$nick = $_POST[nick];

$password = $_POST[password];

if (trim($nick) == '' || trim($password) == '')

$error_string .= 'Please enter your username and password.<br>';
else

{

// ****************Go on an check login details**********************************
$sql="SELECT Username,Password FROM Your_Table WHERE Username='$nick' AND
$Password='$password'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);

if (mysqli_num_rows($result) < 1)
// if user does not exists
$error_string .= 'The Username and Password did not match.<br>';



if ($row['Username'] == $nick && $row['Password'] == $password)

{

/* check and destroy previous cookie if any */



IF (isset($_COOKIE["user"])){

setcookie("user","",time()-360000) ;

}

// Set new session
$i= $row['Username'];

setcookie("user","$i", time()+360000) ;

mysqli_free_result($result);

mysqli_close($con) ;

header('Location: home.php') ;

exit();

}

}
Re: Help On Login Code For Php N How To Store On Data Base. by veltany: 10:40pm On Oct 12, 2013
YOUR DATABASE FILE WILL BE:

$database_host = "YOUR HOST" ;

$database_user = "USER" ;

$database_password = "PASSWORD" ;

$database_name = "NANE";



$con = mysqli_connect($database_host, $database_user, $database_password, $database_name) ;

if (!($con))

exit("CONNECTION ERROR: ".mysqli_connect_error());



function db_query($sql)

{

return mysqli_query($sql, $GLOBALS['con']);

}

?>
Re: Help On Login Code For Php N How To Store On Data Base. by ticabo: 2:01am On Oct 15, 2013
@nollyj and @veltany: Good job guys!

@callydon

This is easily the most properly-written, clean and SECURE PHP Login script on the internet today.

http://www.php-login.net/

Try versions 1 and 2 then when you get more comfortable with PHP, try version 4.

There are many PHP scripts on the internet today that will only make you cry eventually when you get compromised, or will teach you bad coding practices. This is not one of them.

(1) (Reply)

Intelligent Machines: Do We Really Need To Fear The Rise of Robotics? / Freelancing In Web Development / What Is D Equivalent Of Oracle Programme In Nigeria Education Today?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.