0

Hello Friends I am new at forum I have create a simple login page name as index.php with following code:

// I have already starts session by session_start()
$qry="select empCode from relaxo_employee_info where empCode='".$username."' and empPassword='".$password."' and empPost='Executive'";
$result=mysql_query($qry);
if($row=mysql_fetch_array($result))
{
    $_SESSION['UID']=$username;
    echo $_SESSION['UID'];              //prints session data successfully so think session set correctly. 
?>
    <script>self.location.href='executive/order_place.php';</script>
<?php               
}

then at starting of order_place.php I continue the session by session_start() and the following code in it to check valid session

    <?php
              session_start();
              if(isset($_SESSION['UID'])==NULL)   // at this point $_SESSION['UID'] find automatically empty. somehow Its blank completely.
              { 
    ?>
    <script>self.location.href='index.php';</script>  //because of session finds empty it redirects to index.php
    <?php
              }
    ?>

and strange things are happens I just share with u which helps you understand my problem

1) the same code is run on localhost successfully and does not work on my domain 2) sometimes session works successfully but sometimes not with same code without any changes

So guys please solve my problem and help me to come out from this issue

mTorres
  • 3,590
  • 2
  • 25
  • 36
Gangesh Bhat
  • 11
  • 1
  • 8
  • you should change `` to `` – iswinky Aug 06 '14 at 12:43
  • Are you setting session name before ```session_start()```? Maybe you're not giving the same name on both pages? – mTorres Aug 06 '14 at 12:46
  • Thanks for quick response iswinky but I hava already done page redirection with header('location:index.php') but sometimes it's not work on domain even if username & password are correct – Gangesh Bhat Aug 07 '14 at 04:27
  • my problem is now resolve guys thank to each and every one of you I study lots of new things about session from all of you – Gangesh Bhat Aug 07 '14 at 04:47

4 Answers4

1

if(isset($_SESSION['UID'])==NULL)

is kind of a weird approach if you want to compare the $_SESSION variable with NULL. Instead, try

if(is_null($_SESSION['UID']))

and see if the problem still occurs.

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
Robin
  • 132
  • 7
0

if(isset($_SESSION['UID'])==NULL) is where your problem is. If true == null or false == null it's never going to work.

ShawnSQL
  • 69
  • 3
0

isset($_SESSION['UID']) tests to see if your variable is set.
in order for you to test it's value try something:

if( isset($_SESSION['UID']) && trim($_SESSION['UID'])!='' )
{
// execute code here
}
andrew
  • 2,058
  • 2
  • 25
  • 33
0

Try like this...

<?php
      session_start();
      if(isset($_SESSION['UID']))   // check whether session is set or not.
         {
           if($_SESSION['UID'] == NULL)  // check session is NULL
              {
                  header('location:index.php'); // redirect to index.php page
              }
         } 
?>
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
  • @user3914336 don't forget to accept this as answer if it helps you. – Deepu Sasidharan Aug 07 '14 at 06:40
  • hey sorry @next2u but is there any procedure to accept answer I really dont have idea about that because I am new at forum and this was my 1st question only and I have post another question please help regarding that also I will appreciate any help from you http://stackoverflow.com/questions/25176064/how-to-handle-the-user-logout-in-browser-multiple-tab-auto-logout-all-open-tabs – Gangesh Bhat Aug 07 '14 at 07:04
  • @user3914336, just make a tick at right side of this answer :) if can't leave it.. :) – Deepu Sasidharan Aug 07 '14 at 07:07