Created
February 24, 2017 10:44
-
-
Save nikolaykrylov/278d19b98d29557a084a167dd445d214 to your computer and use it in GitHub Desktop.
login/logout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //connect.inc.php | |
$conn_error = 'Could not connect.'; | |
$mysql_host = 'localhost'; | |
$mysql_user = 'root'; | |
$mysql_pass = ''; | |
$mysql_connect = @mysqli_connect($mysql_host, $mysql_user, $mysql_pass); | |
$mysql_db = 'a_database'; | |
if(!@mysqli_connect($mysql_host, $mysql_user, $mysql_pass) || !@mysqli_select_db($mysql_connect, $mysql_db)) | |
{ | |
die($conn_error); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //core.inc.php | |
ob_start(); | |
session_start(); | |
$current_file = $_SERVER['SCRIPT_NAME']; | |
$http_referer = $_SERVER['HTTP_REFERER']; | |
function loggedin() { | |
if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // index.php | |
require 'connect.inc.php'; | |
require 'core.inc.php'; | |
if (loggedin()) { | |
echo 'You\'re logged in <a href = "logout.php">Log out</a>'; | |
} | |
else { | |
include 'loginform.inc.php'; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //loginform.inc.php | |
if (isset ($_POST['username']) && isset ($_POST['password'])) { | |
$username = $_POST['username']; | |
$password = $_POST['password']; | |
$password_hash = md5($password); | |
if (!empty($username) && !empty($password)) { | |
$query="SELECT id FROM users WHERE username= '$username' AND password='$password_hash'"; | |
if ($query_run = mysqli_query ($mysql_connect, $query)) { | |
$query_num_rows = mysqli_num_rows($query_run); | |
if ($query_num_rows == 0) { | |
echo 'Invalid username/password combination.'; | |
} else if ($query_num_rows == 1) { | |
$query_row = mysqli_fetch_assoc($query_run); | |
$user_id = $query_row ['id']; | |
$_SESSION['user_id'] = $user_id; | |
header('Location: index.php'); | |
} | |
} | |
} else { | |
echo 'You must supply username and password.'; | |
} | |
} | |
?> | |
<form action = "<?php echo $current_file; ?>" method = "post"> | |
Username: <input type = "text" name = "username"> Password: <input type = "password" name = "password"> | |
<input type = "submit" value = "Log in"> | |
</form> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'core.inc.php'; | |
session_destroy(); | |
header('Location:' .$http_referer); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Login\logout form