Jump to content

Session Usage


verror

Recommended Posts

So, Sessions are a pretty weak point for me and I just wanted to verify if there is any better method for using them than how I currently am.

 

At the moment this is how I am creating the Session after details have been input (this is only part of the class)

 public function Login() {
  $success = false;
  try{
  $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
  $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  $sql = "SELECT * FROM admin WHERE username = :username AND password = :password LIMIT 1";
                       $user = username;

  $stmt = $con->prepare( $sql );
  $stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
  $stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
  $stmt->execute();

  $valid = $stmt->fetchColumn();

  if( $valid ) {
  $success = true;
                       session_start();
                       session_regenerate_id();
                       $_SESSION['user'] = $user['user'];
                       session_write_close();
                       header("Location: /admin.php");
                       exit();

 

This is how I check the sessions on the secured page:

 

<?php
session_start();
if(!isset($_SESSION['user']) || (trim($_SESSION['user']) == '')) {
header("location: login.php");
   exit();
}
?>

 

And this is how I logout:

 

<?php   
session_start(); 
session_destroy(); 
header("location:/index.php"); 
exit();
?>

 

Is that a decent method, if not, how else should I go about doing it?

Link to comment
https://forums.phpfreaks.com/topic/271513-session-usage/
Share on other sites

Other than the unnecessary call to session_write_close(), and the need to manually unset $_SESSSION on logout. I think that looks good.

You've remembered to regenerate the ID upon a successful login, which is the most important part. (Bar actually getting it to work, of course.) :)

Link to comment
https://forums.phpfreaks.com/topic/271513-session-usage/#findComment-1397081
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.