Jump to content

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'];
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.