Jump to content

[SOLVED] Working with SESSIONS


balkan7

Recommended Posts

hi guys,

I tested the code on my localhost and it works, but when I tested on my site that will not show me $_SESSION[ 'username'];

 

login.php

<?php 
session_start();
session_destroy();

$show = "";

if (isset($_POST['login'])) { 
$username = $_POST['username'];
$md5_password = md5($_POST['password']); 

$result = mysql_query("SELECT * FROM admin WHERE username='$username' AND password='$md5_password'");
  if(dbrows($rezultat) != 0){
    session_register("username"); 
    header("location:admin/index.php"); 
   exit;
  } else {
  $show = $lang['login-005'];
  }
} ?>

 

admin/index.php

<?php 
session_start(); 
if (!session_is_registered("username")) {
header("location:../index.php");
}
echo $_SESSION[ 'username'];
?>

Link to comment
https://forums.phpfreaks.com/topic/148283-solved-working-with-sessions/
Share on other sites

why you got this >>session_destroy(); afther session_start()

 

your need another session_start() afther the session_destroy.

 

 

in fact my posted replies is correct but , you should rely

unset($_SESSION['session_name']) to kill the session needs killing,

but session_start kills all sessions.

 

 

<?php 
ob_start();
session_start();
session_destroy();

session_start();

$show = "";

if (isset($_POST['login'])) { 
$username = $_POST['username'];
$md5_password = md5($_POST['password']); 

$result = mysql_query("SELECT * FROM admin WHERE username='$username' AND password='$md5_password'");
  if(dbrows($rezultat) != 0){
    $_SESSION['username']=$username; 
    header("location:admin/index.php"); 
   exit;
  } else {
  $show = $lang['login-005'];
  }
} 
ob_flush();
?>

 

 

<?php 
session_start(); 
if (! $_SESSION['username']) {
header("location:../index.php");
}
echo $_SESSION[ 'username'];
?>

Do you have different versions of PHP?  session_is_registered has been deprecated in recent versions of PHP.

 

You have to use isset

 

 if (!isset($_SESSION['username'])) {
header("location: ../index.php");
} else {
   echo $_SESSION['username'];
}

 

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.