note 89775 added to function.session-start

From: Date: Mon, 23 Mar 2009 06:09:46 +0000
Subject: note 89775 added to function.session-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-152313@lists.php.net to get a copy of this message
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://es.php.net/explode">"exploding"</a> them .. play arround ... ---- Server IP: 80.64.47.13 Probable Submitter: 79.150.106.77 ---- Manual Page -- http://www.php.net/manual/en/function.session-start.php Edit -- https://master.php.net/note/edit/89775 Del: integrated -- https://master.php.net/note/delete/89775/integrated Del: useless -- https://master.php.net/note/delete/89775/useless Del: bad code -- https://master.php.net/note/delete/89775/bad+code Del: spam -- https://master.php.net/note/delete/89775/spam Del: non-english -- https://master.php.net/note/delete/89775/non-english Del: in docs -- https://master.php.net/note/delete/89775/in+docs Del: other reasons-- https://master.php.net/note/delete/89775 Reject -- https://master.php.net/note/reject/89775 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152313) next »