note 89775 deleted from function.session-start by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 22:15:21 +0000 |
| Subject: | note 89775 deleted from function.session-start by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208342@lists.php.net to get a copy of this message | ||
Note Submitter: microvalen at NOSPAM dot microvalen dot com
----
I have tried many times a lot of login scripts but sometimes when you have a lot of traffic you can
note the "overloading" of the server tring to deal with all sessions specialy with the
long, long scripts and classes ... checking all stuffs (ids, ips, usernames, etc) the loading page
increase ...specialy when you have a "home" server with a small blog..
if you open the folder "/tmp" you will thousands of files that do nothing else that
increasing the size of the folder.
Sessions are good and I just offer an alternative.
.. and for that I just created in about 20 min a simple script that can handle a simple login.
<b>(Be aware !! this is just an example .. it not make use of any check of xss atacks or other
scripts !!!</b>
and make use of cookies [today all moderns browsers use cookies])
<?php
$mysqli = new mysqli('localhost', 'root', 'pass',
'nameofDB');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit(); }
$cookie = (isset($_COOKIE['vuid'])) ? $_COOKIE['vuid'] : '';
if(!$cookie) {
$user_level = 0;
setcookie('vuid', '0.' . md5('anonymous')); }
else {
$part = explode('.', $cookie);
$user_level = (int) $part[0];
$user_name = (string) $part[1];
if($user_level != 0) {
$is_logged = check_login($user_level, $user_name); }
else {
$is_logged = false; } }
if(isset($_GET['login'])) {
// here you MUST make a form and a function that realy check the login ...
if($YourFunctionCheckIsOk) {
setcookie('vuid', /*levels betwen 1-9*/ '9.' .
md5('PutTheUserNameHere'));
echo '<meta http-equiv="refresh" content="1;URL=index.php">
Correct Login<br>
You will be redirected to the main page'; } }
elseif(isset($_GET['logout'])) {
setcookie('vuid', '0.' . md5('anonymous'));
echo '<meta http-equiv="refresh" content="1;URL=index.php">
You are out now ... see you soon<br>
You will be redirected to the main page'; }
else {
if($is_logged) {
echo 'Welcome ' . USERNAME . ' <a
href="?logout">LOGOUT</a>'; }
else {
echo 'You are a Anonymous. LOGIN <a href="?login">HERE</a>'; } }
function check_login($user_level, $user_name) {
global $mysqli;
$is_logged = false;
$result = $mysqli->query('SELECT nick FROM accounts WHERE user_level = ' .
$user_level);
$row = $result->fetch_array();
$result->close();
if($user_name === md5($row['nick'])) {
define('USERNAME', $row['nick']);
return $is_logged = true; }
return $is_logged = false; }
$mysqli->close();
?>
PS: you can change the nick with the ip or better use both !
like:
"level.md5(sha1(nick)).sha1(md5(ip)) etc ...
"0.4aea7c7d495676.4aea7c7d495676"
and check them separetly by <a href="http://php.net/explode">"exploding"</a>
them ..
play arround ...