jun 12
This commit is contained in:
20
php/connect.inc.php
Normal file
20
php/connect.inc.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
//How to connect to MySQL
|
||||
|
||||
$mysql_host = 'localhost';
|
||||
$mysql_user = 'example_user';
|
||||
$mysql_pass = 'Kolosnjaj4321!';
|
||||
|
||||
$conn_error = 'Could not connect to database!';
|
||||
$mysql_db = 'example_database';
|
||||
|
||||
$con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass);
|
||||
|
||||
if ($con) {
|
||||
if (mysqli_select_db($con, $mysql_db)) {
|
||||
|
||||
} else {
|
||||
die($conn_error);
|
||||
}
|
||||
}
|
||||
?>
|
||||
26
php/core.inc.php
Normal file
26
php/core.inc.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
ob_start();
|
||||
session_start();
|
||||
$current_file = $_SERVER['SCRIPT_FILENAME'];
|
||||
|
||||
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])){
|
||||
$http_referer = $_SERVER['HTTP_REFERER'];
|
||||
}
|
||||
|
||||
function loggedin(){
|
||||
if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function getuserfield($field){
|
||||
global $con;
|
||||
$query = "SELECT `$field` FROM `globaly_accounts` WHERE `id`= '" . $_SESSION['user_id']. "'";
|
||||
if($query_run = mysqli_query($con,$query)){
|
||||
$row = mysqli_fetch_assoc($query_run);
|
||||
return $field = $row[$field];
|
||||
}
|
||||
}
|
||||
?>
|
||||
12
php/index.php
Normal file
12
php/index.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
require 'connect.inc.php';
|
||||
require 'core.inc.php';
|
||||
|
||||
if (loggedin()) {
|
||||
$name = getuserfield('name');
|
||||
echo ' You are logged in '. $name.' <a href="logout.php">Log out</a><br>';
|
||||
} else {
|
||||
include 'login.php';
|
||||
}
|
||||
?>
|
||||
31
php/login.php
Normal file
31
php/login.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
require 'connect.inc.php';
|
||||
require 'core.inc.php';
|
||||
|
||||
if (isset($_POST['email']) && isset($_POST['password'])) {
|
||||
$email = $_POST['email'];
|
||||
$password = $_POST['password'];
|
||||
$password_hash = md5($password);
|
||||
|
||||
if (!empty($email) && !empty($password)) {
|
||||
$query = "SELECT `id` FROM `globaly_accounts` WHERE `email` ='" . mysqli_real_escape_string($con, $email) .
|
||||
"' AND `password` = '" .mysqli_real_escape_string($con, $password_hash )."'";
|
||||
if ($query_run = mysqli_query($con, $query)) {
|
||||
$query_num_rows = mysqli_num_rows($query_run);
|
||||
if ($query_num_rows == 0) {
|
||||
echo 'invalid email/password combination';
|
||||
} else if ($query_num_rows == 1) {
|
||||
//treba nam $user_id
|
||||
$row = mysqli_fetch_assoc($query_run);
|
||||
$user_id = $row['id'];
|
||||
|
||||
$_SESSION['user_id'] = $user_id;
|
||||
header('Location: index.php');
|
||||
}
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
echo 'You must suply email or password';
|
||||
}
|
||||
}
|
||||
?>
|
||||
8
php/logout.php
Normal file
8
php/logout.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
require 'core.inc.php';
|
||||
|
||||
session_destroy();
|
||||
header('Location: '. '../login.html');
|
||||
|
||||
?>
|
||||
52
php/register.php
Normal file
52
php/register.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
require 'connect.inc.php';
|
||||
require 'core.inc.php';
|
||||
|
||||
if (!loggedin()) {
|
||||
if (
|
||||
isset($_POST['name']) && isset($_POST['email'])
|
||||
&& isset($_POST['password']) && isset($_POST['password_again'])
|
||||
) {
|
||||
$name = $_POST['name'];
|
||||
$email = $_POST['email'];
|
||||
$password = $_POST['password'];
|
||||
$password_again = $_POST['password_again'];
|
||||
$password_hash = md5($password);
|
||||
$upperCase = preg_match('/[A-Z]/', $password);
|
||||
|
||||
if (
|
||||
!empty($name) && !empty($password) && !empty($password_again) &&
|
||||
!empty($email)
|
||||
) {
|
||||
if (strlen($name) > 30) {
|
||||
echo 'Please ahear to maxlength of fields';
|
||||
} else {
|
||||
if ($password != $password_again) {
|
||||
echo 'Passwords do not match';
|
||||
}elseif (strlen($password) < 6){
|
||||
echo 'Password is weak!';
|
||||
}
|
||||
else {
|
||||
$query = "SELECT `email` FROM `globaly_accounts` WHERE `email` ='$email'";
|
||||
$query_run = mysqli_query($con, $query);
|
||||
if (mysqli_num_rows($query_run) == 1) {
|
||||
echo 'The email ' . $email . 'already exists';
|
||||
} else {
|
||||
$query = "INSERT INTO `globaly_accounts` VALUES (NULL ,'" . mysqli_real_escape_string($con, $name) .
|
||||
"','" . mysqli_real_escape_string($con, $email) . "','" . mysqli_real_escape_string($con, $password_hash) . "')";
|
||||
if ($query_run = mysqli_query($con, $query)) {
|
||||
echo '<script>alert("You are registerd!")</script>';
|
||||
header('Location: ../index.html');
|
||||
} else {
|
||||
echo 'We could not register you!';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo 'All fields are required';
|
||||
}
|
||||
}
|
||||
} else if (loggedin()) {
|
||||
echo 'You are already registered and logged in !';
|
||||
}
|
||||
5
php/register_success.php
Normal file
5
php/register_success.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
echo 'Registered!'
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user