1
Reply

using oops to make login and registration page

Krishan Sahani

Krishan Sahani

Jun 7 2017 9:40 AM
261
when i run the register.php code form not show in browser
 
this is my whole code..
 
class.php
<?php
include ("config.php");

class User{

public $db;

public function __construct()
{

$this->db = new mysqli('localhost', 'root', 'iws123#', 'register');

if(mysqli_connect_errno())
{

echo "Error: Could not connect to database.";

exit;

}

}



/*** for registration process ***/

public function reg_user($first_name,$last_name,$username,$email,$password,$type)
{

$sql="SELECT * FROM user_table2 WHERE username='$username'";



//checking if the username is available in db

$check = $this->db->query($sql) ;

$count_row = $check->num_rows;



//if the username is not in db then insert to the table

if ($count_row == 0)
{

$sql1="INSERT INTO user_table2 SET first_name='".$first_name."',last_name ='".$last_name."', username='".$username."',email='".$email."',password='".$password."',type='".$type."'";

$result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");

return $result;

}

else
{
return false;
}

}



/*** for login process ***/

public function check_login($username, $password){


$sql2="SELECT id from user_table2 WHERE username='$username' and password='$password'";



//checking if the username is available in the table

$result = mysqli_query($this->db,$sql2);

$user_data = mysqli_fetch_array($result);

$count_row = $result->num_rows;



if ($count_row == 1) {

// this login var will use for the session thing

$_SESSION['login'] = true;

$_SESSION['id'] = $user_data['id'];

return true;

}
else{

return false;

}

}


/*** starting the session ***/

public function get_session()
{

return $_SESSION['login'];

}



public function user_logout() {

$_SESSION['login'] = FALSE;

session_destroy();

}



}
?>
register.php
 
<?php
include ("class.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$user = new User(); // Checking for user logged in or not

if (isset($_REQUEST['submit']))
{
//extract($_REQUEST);

$register = $user->reg_user($first_name,$last_name $username,$email,$password,$type );
if ($register)
{

echo 'Registration successful <a href="login.php">Click here</a> to login';
}
else
{
echo 'Registration failed.Username already exits';
}
}


?>
<html>
<head>
</head>
<style>
#container{width:400px; margin: 0 auto;}
</style>

<script type="text/javascript" language="javascript">
function submitreg() {
var form = document.reg;
if(form.first_name.value == ""){
alert( "Enter firstname." );
return false;
}
else if(form.last_name.value == ""){
alert( "Enter lastname." );
return false;
}
else if(form.username.value == ""){
alert( "Enter username." );
return false;
}
else if(form.email.value == ""){
alert( "Enter email." );
return false;
}
else if(form.password.value == ""){
alert( "Enter password." );
return false;
}
else if(form.type.value == ""){
alert( "Enter type." );
return false;
}
}
</script>
<div id="container">
<h1>Registration Here</h1>
<form action="" method="post" name="reg">
<table>
<tbody>
<tr>
<th>Firstname:</th>
<td><input type="text" name="first_name" required="" /></td>
</tr>
<tr>
<th>Lastname:</th>
<td><input type="text" name="last_name" required="" /></td>
</tr>
<th>Username:</th>
<td><input type="text" name="username" required="" /></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="email" required="" /></td>
</tr>
<tr>
<th>Password:</th>
<td><input type="text" name="password" required="" /></td>
</tr>
<th>Type:</th>
<td><input type="text" name="type" required="" /></td>
</tr>
<tr>
<td></td>
<td><input onclick="return(submitreg());" type="submit" name="submit" value="Register" /></td>
</tr>
<tr>
<td></td>
<td><a href="login.php">Already registered! Click Here!</a></td>
</tr>
</tbody>
</table>
</form></div>
</html>

login.php
 
<?php
session_start();
include ("class.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$user = new User();

if (isset($_REQUEST['submit'])) {
//extract($_REQUEST);
$login = $user->check_login($username, $password);
if ($login)
{
// Registration Success
header("location:home.php");
}
else {
// Registration Failed
echo 'Wrong username or password';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style><!--
#container{width:400px; margin: 0 auto;}

--></style>

<script type="text/javascript" language="javascript">

function submitlogin() {
var form = document.login;
if(form.username.value == ""){
alert( "Enter username." );
return false;
}
else if(form.password.value == ""){
alert( "Enter password." );
return false;
}
}

</script>

<span style="font-family: 'Courier 10 Pitch', Courier, monospace; font-size: 13px; font-style: normal; line-height: 1.5;"><div id="container"></span>
<h1>Login Here</h1>
<form action="" method="post" name="login">
<table>
<tbody>
<tr>
<th>Username:</th>
<td><input type="text" name="username" required="" /></td>
</tr>
<tr>
<th>Password:</th>
<td><input type="password" name="password" required="" /></td>
</tr>
<tr>
<td></td>
<td><input onclick="return(submitlogin());" type="submit" name="submit" value="Login" /></td>
</tr>
<tr>
<td></td>
<td><a href="register.php">Register new user</a></td>
</tr>
</tbody>
</table>
</form></div>
</head>
</html>



home.php
 
<?php
session_start();
include ('class.php');
$user = new User();
$id = $_SESSION['id'];
if (!$user->get_session())
{
header("location:login.php");
}

if (isset($_GET['q']))
{
$user->user_logout();
header("location:login.php");
}
?>
<html>
<head>
<title></title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
Home
<style><!--
body{
font-family:Arial, Helvetica, sans-serif;
}
h1{
font-family:'Georgia', Times New Roman, Times, serif;
}
--></style>
<div id="container">
<div id="header"><a href="home.php?q=logout">LOGOUT</a></div>
<div id="main-body">
<h1>Hello <?php $user->get_username($id); ?></h1>
</div>
<div id="footer"></div>
</div>

<body>

</body>
</html>

 

Answers (1)