move php functions into functions file

This commit is contained in:
Andreas Zweili 2017-02-06 21:55:36 +01:00
parent 137ce0be7e
commit 718398d824
6 changed files with 86 additions and 133 deletions

50
functions.php Normal file
View File

@ -0,0 +1,50 @@
<?php
require('db.php');
function login ()
{
session_start();
if (isset($_REQUEST['username'])) {
//Checking is user existing in the database or not
global $con;
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$query = "SELECT * FROM users WHERE userLogin='$username'
and userPass='$password'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if ($rows==1) {
$_SESSION['username'] = $username;
// Redirect user to home.php
header("Location: home.php");
} else {
echo "<h3>Username/password is incorrect.</h3>";
}
}
}
// If form submitted, insert values into the database.
function register ()
{
if (isset($_REQUEST['username'])) {
global $con;
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$email = stripslashes($_REQUEST['email']);
$email = mysqli_real_escape_string($con,$email);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$query = "INSERT into users (userLogin, userPass, userEmail)
VALUES ('$username', '$password', '$email')";
$result = mysqli_query($con,$query);
if ($result) {
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='index.php'>Login</a></div>";
}
}
}
?>

View File

@ -1,3 +1,6 @@
<?php
include 'functions.php';
?>
<!DOCTYPE html>
<html>
<head>
@ -20,12 +23,12 @@
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit"/>
<?php
include 'login.php';
?>
<input name="submit" type="submit" value="Login"/>
</form>
<p>Not registered yet? <a href='registrationindex.php'>Register Here</a></p>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>
<?php
login();
?>
</div>
</body>
</html>

View File

@ -1,27 +0,0 @@
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])) {
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username'
and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if ($rows==1) {
$_SESSION['username'] = $username;
// Redirect user to home.php
header("Location: home.php");
} else {
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}
?>

View File

@ -1,45 +1,37 @@
<?php
include 'functions.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/loginstyle.css" />
<!-- enable utf-8 encoding for umlauts etc.-->
<meta charset="utf-8">
<!-- Description of what this dose -->
<meta name ="viewport" content="width=device-width, initial-scale=1">
<!-- link to the default css file -->
<link rel="stylesheet" href="css/loginstyle.css"/>
</head>
<body>
<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_REQUEST['username'])) {
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$email = stripslashes($_REQUEST['email']);
$email = mysqli_real_escape_string($con,$email);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."',
'$email', '$trn_date')";
$result = mysqli_query($con,$query);
if ($result) {
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
} else {
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
<div>
<div>
<header>
<!-- The title begins here -->
<h1>WebShop NeXt</h1>
</header>
</div>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register"/>
</form>
<?php
register();
?>
</div>
</div>
<?php } ?>
</body>
</html>

View File

@ -1,34 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<!-- enable utf-8 encoding for umlauts etc.-->
<meta charset="utf-8">
<!-- Description of what this dose -->
<meta name ="viewport" content="width=device-width, initial-scale=1">
<!-- link to the default css file -->
<link rel="stylesheet" href="css/loginstyle.css"/>
</head>
<body>
<div>
<div>
<header>
<!-- The title begins here -->
<h1>WebShop NeXt</h1>
<!-- The title begins here -->
<!-- The sidebar naviagtion begins here -->
<nav>
<!--Registration-->
<?php
include 'registration.php';
?>
<!--Registration end-->
</nav>
<!-- The sidebar naviagtion ends here -->
</header>
</div>
</div>
</body>
</html>

View File

@ -1,31 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<!-- enable utf-8 encoding for umlauts etc.-->
<meta charset="utf-8">
<!-- Description of what this dose -->
<meta name ="viewport" content="width=device-width, initial-scale=1">
<!-- link to the default css file -->
<link rel="stylesheet" href="css/stylesheet.css"/>
</head>
<body>
<div>
<div>
<header>
<!-- The sidebar naviagtion begins here -->
<nav>
<ul>
<li><a href="ticket.html">Ticket</a></li>
<li><a href="board.html">Board</a></li>
<li><a class="active" href="index.html">Home</a></li>
</ul>
</nav>
<!-- The sidebar naviagtion ends here -->
</header>
</div>
</div>
<p>Some title</p>
</body>
</html>