session in php

Thread Solved

Join Date: Jul 2009
Posts: 45
Reputation: AirGear is an unknown quantity at this point 
Solved Threads: 2
AirGear AirGear is offline Offline
Light Poster

session in php

 
0
  #1
Nov 4th, 2009
i'm a newbie in php and maybe my question is so silly.

i tried to create a session in a file named login.php
  1. <?php
  2. include "koneksi.inc.php";
  3. $name=$_POST['name'];
  4. $password=$_POST['password'];
  5.  
  6. $hasil=mysql_query("SELECT * FROM TabelPegawai WHERE nama='$name' AND password='$password'");
  7. $row=mysql_fetch_array($hasil);
  8.  
  9. if ($row[nama]==$name AND $row[password]==$password)
  10. {
  11. session_start();
  12. $_SESSION['namauser']=$row[nama];
  13. $_SESSION['passuser']=$row[password];
  14. $_SESSION['level']=$row[jabatan];
  15.  
  16. if($_SESSION['level']=="Manager")
  17. {header("location: manager.php");}
  18. else if($_SESSION['level']=="Kasir")
  19. {header("location:kasir.php");}
  20. else if($_SESSION['level']=="Gudang")
  21. {header("location:gudang.php");}
  22. else
  23. {header("location:http://www.google.com");}
  24. }
  25. else
  26. {header("location:http://www.yahoo.com");}
  27. ?>

but, $_SESSION['level'] is not recognized in other files like manager.php, gudang.php, and kasir.php

is there something wrong with my code?
thanks for your helps.
Last edited by AirGear; Nov 4th, 2009 at 3:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 73
Reputation: filch is an unknown quantity at this point 
Solved Threads: 1
filch filch is offline Offline
Junior Poster in Training
 
0
  #2
Nov 4th, 2009
Did you start the session on all the pages where you need to access your session variables?

Dave
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: AirGear is an unknown quantity at this point 
Solved Threads: 2
AirGear AirGear is offline Offline
Light Poster
 
0
  #3
Nov 4th, 2009
Originally Posted by filch View Post
Did you start the session on all the pages where you need to access your session variables?

Dave
no, i only start the session in login.php
do i need to start the session in every page?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: AirGear is an unknown quantity at this point 
Solved Threads: 2
AirGear AirGear is offline Offline
Light Poster
 
0
  #4
Nov 4th, 2009
oh, it works if i start the session in every page that needs session variables. i also must start the session in a php file that destroy the session right? like this :
  1. <?php
  2. session_start();
  3. session_destroy();
  4. ?>

am i right?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 73
Reputation: filch is an unknown quantity at this point 
Solved Threads: 1
filch filch is offline Offline
Junior Poster in Training
 
0
  #5
Nov 4th, 2009
Originally Posted by AirGear View Post
oh, it works if i start the session in every page that needs session variables. i also must start the session in a php file that destroy the session right? like this :
  1. <?php
  2. session_start();
  3. session_destroy();
  4. ?>

am i right?
Yes but obviously, not until you are finished with it. You should also test whether a session has already been started or not before you try and start another one i.e.

  1. if (!isset($_SESSION)) {
  2. session_start();
  3. }

Dave
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,078
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #6
Nov 4th, 2009
You don't need to check. session_start() will not start a new session overwriting the old one. It just propagates.
session_destroy wouldn't be used very often, unless in a logout page or similar. You wouldn't have a session_start in this file as well (I don't think).
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 73
Reputation: filch is an unknown quantity at this point 
Solved Threads: 1
filch filch is offline Offline
Junior Poster in Training
 
0
  #7
Nov 4th, 2009
Originally Posted by ardav View Post
You don't need to check. session_start() will not start a new session overwriting the old one. It just propagates.
session_destroy wouldn't be used very often, unless in a logout page or similar. You wouldn't have a session_start in this file as well (I don't think).
OK .. that's your choice. It is simply good practice.

D
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: AirGear is an unknown quantity at this point 
Solved Threads: 2
AirGear AirGear is offline Offline
Light Poster
 
0
  #8
Nov 5th, 2009
thanks guys. i think i still need to insert session_start() in logout php, i have tried it.
@Dave : thanks so much for the solution
@Ardav : thanks (again)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC