944,154 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 650
  • PHP RSS
Nov 4th, 2009
0

session in php

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AirGear is offline Offline
62 posts
since Jul 2009
Nov 4th, 2009
0
Re: session in php
Did you start the session on all the pages where you need to access your session variables?

Dave
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 4th, 2009
0
Re: session in php
Click to Expand / Collapse  Quote originally posted by filch ...
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?
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AirGear is offline Offline
62 posts
since Jul 2009
Nov 4th, 2009
0
Re: session in php
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 :
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. session_destroy();
  4. ?>

am i right?
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AirGear is offline Offline
62 posts
since Jul 2009
Nov 4th, 2009
0
Re: session in php
Click to Expand / Collapse  Quote originally posted by AirGear ...
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 :
PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  1. if (!isset($_SESSION)) {
  2. session_start();
  3. }

Dave
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 4th, 2009
-1
Re: session in php
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).
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 4th, 2009
0
Re: session in php
Click to Expand / Collapse  Quote originally posted by ardav ...
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
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 5th, 2009
0
Re: session in php
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)
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AirGear is offline Offline
62 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP echo email address as mailto
Next Thread in PHP Forum Timeline: redirecting in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC