Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Clase para manejo de sesiones seguras.
- *
- * @version: 1.0
- *
- * @author: Juan Correa.
- *
- */
- class Session
- {
- public $session_id;
- private $session_token;
- public function __construct()
- {
- $this->initSession();
- $this->setSessionToken();
- $this->setSessionValue('_session_token_', $this->session_token);
- }
- /*
- * Configura caché de sesión y la inicializa
- *
- * @params: void
- *
- * @return: void
- */
- private function initSession()
- {
- $this->setSessionCacheLimiter('private');
- $this->setSessionCacheExpire(0);
- $this->setCookieParams();
- session_start();
- $this->sessionRegenerateId();
- }
- /*
- * Establece el limitador del caché actual
- *
- * @params: String limiter: el limitador
- *
- * @return: void
- */
- private function setSessionCacheLimiter($limiter)
- {
- session_cache_limiter($limiter);
- }
- /*
- * Establece la caducidad del caché en minutos
- *
- * @params: Int minutes: duración del caché
- *
- * @return: void
- */
- private function setSessionCacheExpire($minutes)
- {
- session_cache_expire($minutes);
- }
- /*
- * Establece los parámetros de la cookie. Su efecto dura lo mismo que el script invocador
- *
- * @params: Int minutes: duración del caché
- *
- * @return: void
- */
- private function setCookieParams()
- {
- $cookie_params = session_get_cookie_params();
- session_set_cookie_params(
- $cookie_params['path'],
- $cookie_params['domain'],
- 'SECURE',
- true);
- }
- /*
- * Crea un token personalizado para mayor seguridad
- *
- * @params: void
- *
- * @return: void
- */
- private function setSessionToken()
- {
- $this->session_token = sha1($this->session_id . 'EscudoDeSeguridad');
- }
- /*
- * Asigna el id de sesión al atributo session_id
- *
- * @params: void
- *
- * @return: void
- */
- public function setSessionId()
- {
- $this->session_id = session_id();
- }
- /*
- * Recupera el valor de session_id
- *
- * @params: void
- *
- * @return: void
- */
- public function getSessionId()
- {
- return $this->session_id;
- }
- /*
- * Crea un nuevo valor al array $_SESSION
- *
- * @params:
- * String name_key: nombre de la llave del array de sesión
- * String value: el valor asociado a la llave
- *
- * @return: void
- */
- public function setSessionValue($name_key, $value)
- {
- $_SESSION[$name_key] = $value;
- }
- /*
- * Recupera un elemento del array $_SESSION
- *
- * @params: String session_value: la llave del array a recuperar
- *
- * @return: el valor del elemento del array solicitado. Si no existe, false
- */
- public function getSessionValue($session_value)
- {
- if (! empty($_SESSION[$session_value]))
- return $_SESSION[$session_value];
- return false;
- }
- /*
- * Elimina un elemento del array $_SESSION
- *
- * @params: String session_value: la llave del array a eliminar
- *
- * @return: void
- */
- public function removeSessionValue($session_value)
- {
- if (! empty( $_SESSION[$session_value]))
- unset ($_SESSION[$session_value]);
- }
- /*
- * Valida sesión iniciada usando session_token y session_id
- *
- * Al autogenerarse al instanciar, siempre deben ser iguales
- *
- * @params: void
- *
- * @return: true en caso de éxito, false lo contrario
- */
- public function checkSession()
- {
- if ($this->session_token === $_SESSION['_session_token_'] and $this->session_id === session_id() )
- return true;
- return false;
- }
- /*
- * Regenera el session_id cuando se deje de hacer referencia al objeto
- *
- * @params: void
- *
- * @return: void
- */
- public function __destruct()
- {
- $this->sessionRegenerateId();
- }
- /*
- * Regenera el session_id
- *
- * @params: void
- *
- * @return: void
- */
- private function sessionRegenerateId()
- {
- session_regenerate_id();
- $this->setSessionId();
- }
- /*
- * Borra los datos y destruye la sesión.
- *
- * @params: void
- *
- * @return: void
- */
- public function destroy()
- {
- $this->session_id = '';
- session_unset();
- session_destroy();
- }
- }
- ?>
- <html>
- <head>
- <title>LogIn</title>
- <link rel="stylesheet" type="text/css" href="Themes/BlueNote/Style.css">
- <?php
- require_once('db.php');
- require_once('sesionsegura.php');
- $MiSesion = new Session();
- if (isset($_POST['userid'])){
- $User = $DB->real_escape_string($_POST['userid']);
- $Password = $DB->real_escape_string($_POST['password']);
- // query to find
- $query = "SELECT admin
- FROM Users WHERE user_id = '" . $User . "' and password = sha2('" . $Password . "', 224)";
- // execute query
- $res = $DB->query($query);
- if ($res->num_rows > 0){
- $_SESSION['userid'] = $User;
- $row = $res->fetch_assoc();
- $_SESSION['admin'] = $row['admin'];
- if ($_SESSION['admin'] == 1)
- header("Location: onconshome.php"); // Redirect user to index2.php
- else
- header("Location: onconshome.php"); // Redirect user to index.php
- }else
- echo "<div class='form'><h3>Username/password is incorrect.</h3></div>";
- }
- ?>
- </head>
- <body bgcolor="#FFFFF7" text="#000000" link="#000000" vlink="#000000" alink="#FF0000" class="CobaltPageBODY">
- <p>
- <form name="LoginName" method="POST">
- <font class="BlueNoteFormHeaderFont">Log In </font><input type="hidden" value="{FormState}" name="FormState">
- <div style="OVERFLOW-Y: auto; OVERFLOW-X: auto; WIDTH: 550px; HEIGHT: 200px">
- <table class="BlueNoteFormTABLE" cellpadding="4">
- <tr>
- <td class="BlueNoteColumnTD" nowrap>
- UserID:
- <br>
- <input type="text" id="userid" name="userid" placeholder="userid"/>
- <br>
- Password :
- <br>
- <input type="password" id="password" name="password" placeholder="password" />
- <br><br>
- <input type="submit" value="Submit" class""submit"/></td>
- </tr>
- </table>
- </div>
- </form>
- <!-- END EditableGrid activity_type -->
- </p>
- </body>
- </html>
- <?php
- $DB = new mysqli("localhost:3306", "root", "", "luncz986_sec_title");
- if (mysqli_connect_errno()) /* check connection */
- die("Connect failed: ".mysqli_connect_error());
- return $DB;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement