1 Replies - 437 Views - Last Post: 11 April 2012 - 03:01 PM Rate Topic: -----

#1 so0oma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 93
  • Joined: 01-January 11

problem in session ?

Posted 11 April 2012 - 02:29 PM

hi,
i developed website for employee
and every thing was correct ... but now when i open the pages many error come
and some pages don't appear as previous ..
the main problem as
Notice: Undefined index: user
Notice: Undefined index: admin
Notice: Undefined index: dmd
and i create session for each one
if($row['Position'] == "admin"){
        session_start();
	   $_SESSION['admin'] = $row['username'];
	   header('Location:mainpage.php');
		exit;


Is This A Good Question/Topic? 0
  • +

Replies To: problem in session ?

#2 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 245
  • View blog
  • Posts: 719
  • Joined: 24-November 08

Re: problem in session ?

Posted 11 April 2012 - 03:01 PM

session_start(); should be called at the beginning of your script, before anything else.

ie:

<?php
session_start();

/* rest of your code */

?>



Undefined index is more of a notice than a problem. It simply means that you are assigning something to a variable that you haven't yet defined. This can be solved by using the isset function.

<?php
session_start();
if(!isset($row)){
  echo "row has not been set";
}else{
  if($row['Position'] == "admin"){
     $_SESSION['admin'] = $row['username'];
     header('Location:mainpage.php');
     exit();
  }
}
?>



That isn't tested but is basically an example of using isset to check whether or not a variable is set before trying to assign other variables. This is a good way to check whether or not your query has been successful and is returning the data you want.

This post has been edited by Slice: 11 April 2012 - 03:03 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1