Login session

Okay …

I just make a simple login then it works okay …meaning that only registered user will be obtained to access the restricted page…but my problem is when I just success login then i click the link to the restricted page from the index page it seem it still recognise as not registered…so the result it will redirect me to the login page back…it’s seem it not bring the session to the restricted page as well…

thanks i advanced

i haven’t opened the file but are you doing a session_start() at the top of every page? Have you checking yoru spelling?

Post the code on the page.

Ok guys …

this is the code I’m using…sorry for the late reply…suppose to after success login it will redirect user to restrict page but it seems it not going anywhere…


//in login.php

<?
/********************************************************/
function db_connection2($dbname){
    $link=mysql_connect("localhost","root");
    mysql_select_db($dbname) or die(mysql_error());
   
    return $link;
}

function db_select2($query){
    $result=mysql_query($query) or die(mysql_error());
    $rows=mysql_num_rows($result);
    return $rows;
    mysql_free_result($result);
}

function db_update2($query){
    mysql_query($query) or die(mysql_error());
}

global $USNAME;

// ----------------------------------------------------------------------------------------
// functions
// ----------------------------------------------------------------------------------------

      function auth(){ 
          global $PHP_SELF, $sent, $mysession, $login, $passwd, $bye, $fail, $status, $action; 

          if ($bye) {                                     // user requested logout

            session_start();
            session_unregister("mysession");
            session_destroy();
            return 0;
          }

          if(isset($sent)):                               // arrive from login form
            $login_ok = 0; 
            if (isset($login) and isset($passwd)): 
           
            $db=db_connection2("mydtbase");
            $rows=db_select2("SELECT * FROM admin WHERE idpengguna='$login' AND katalaluan='$passwd' LIMIT 1"); //verify admin first time
            $radmin=db_select2("SELECT * FROM admin WHERE idpengguna='$login' AND katalaluan='$passwd' AND us_type='Superuser' LIMIT 1"); //check superuser
                       
              if ($rows==1):                            //was replaced
                  session_start(); 
                // create the session array
                  $mysession = array ("login" => $login, "passwd" => $passwd, "ID" => session_id(), "valid" => 1, "type" => $radmin); 
                  session_register("mysession"); 
                  $tempsesid=$mysession["ID"];
                  db_update2("UPDATE admin SET masa=NOW(), session_id='$tempsesid' WHERE idpengguna='$login'"); //last update
                 
                 
                  return 1;                               // authentication succeeded
                  $login_ok = 1; 
                 
                  break; 
                endif; 
             // endwhile;
             mysql_close($db);
            endif; 
            if(!$login_ok): 
              return 0;                                   // access denied
            endif; 

          else:                                           // arrive from session var
            $login_ok = 0; 
            session_start(); 
            foreach($GLOBALS["mysession"] as $elem):      // retrieve session array
             $ses_tmp[] = $elem; 
            endforeach; 
            $login = $ses_tmp[0];                         
            $passwd = $ses_tmp[1];                      
   
            $db=db_connection2("mydtbase");
            $rows=db_select2("SELECT * FROM admin WHERE idpengguna='$login' AND katalaluan='$passwd' LIMIT 1");
            //$radmin=db_select2("SELECT * FROM admin WHERE idpengguna='$login' AND katalaluan='$passwd' AND us_type='1' LIMIT 1");
           
            if ($rows==1):                               
                session_start(); 
                // create the session array
                $mysession = array ("login" => $login, "passwd" => $passwd, "ID" => session_id(), "valid" => 1, "type" => "1"); 
                session_register("mysession"); 
                //db_update2("UPDATE userprofile SET us_lastenter=NOW() WHERE us_username='$login'");
               
                return 1;                                 // authentication succeeded
                $login_ok = 1; 
               
                break; 
              endif; 
           // endwhile; 
           mysql_close($db);
            if(!$login_ok): 
              return 0;                                   // access denied
            endif; 
          endif; 
      } 

      function LoginForm(){ 
            global $PHP_SELF; 
            ?> 
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <html>
            <head>
              <title>:: LOGIN FLASH SHOPPING CART ::</title>
            <STYLE type=text/css>
            .text {
    PADDING-RIGHT: 10px; PADDING-LEFT: 20px
}
TD {
    FONT-SIZE: 11px; FONT-FAMILY: Tahoma,sans-serif
}
LI {
    LIST-STYLE-IMAGE:  url(img/pic.jpg)}
A {
    TEXT-DECORATION: none
}
A:hover {
    TEXT-DECORATION: underline
}
.bg {
    BACKGROUND-POSITION: 50% top; BACKGROUND-REPEAT: no-repeat
}
.textfield {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    color: #003366;
}
.style2 {
    font-size: 18px;
    color: #CCCCCC;
    font-weight: bold;
    font-family: Plasmatica;
}
            </STYLE>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
           
<body bgcolor="#CCCCCC">
<br>
<br>
<br>
<br>
<br>
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr valign="top">
    <td bgcolor="#990000">
      <div align="center"><span class="style2"><br>
        LOGIN PAGE </span><font color="#FF9900"><font size="2"><br>
        <br>
      </font> </font></div>
    </td>
  </tr>
  <tr valign="top">
    <td bgcolor="#dcdbd5">
      <div align="center"><br>
        <table width="40%" border="0" cellpadding="0" cellspacing="1" bgcolor="#6699FF">
<form method="post" action="<?php echo basename($PHP_SELF); ?>" name=loginform>
            <tr>
              <td bgcolor="#292B2A"> <table width="100%" border="0" cellspacing="1" cellpadding="0">
                  <tr>
                    <td width="86%" bgcolor="#f5f5f5" height="15">
                      <div align="center"><b><br>
                        <font size="2">&nbsp;&nbsp;&nbsp;&nbsp;Id Pengguna </font>:
                        <input type="text" name="login" class="textfield" size="16">
                        <br>
                        <br>
                        <font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Katalaluan
                        :</font>
                        <input type="password" name="passwd" class="textfield" size="16">
                        <br>
                        <br>
                        <input name="sent" type="submit" class="textfield" value="Login">
                        &nbsp;
                        <input name="Reset" type="reset" class="textfield" value="Reset">
                        <br>
                        <br>
                        </b></div>
                    </td>
                  </tr>
                </table></td>
            </tr>
          </form>
        </table>
     
        <br>
      </div>
    </td>
  </tr>
  <tr valign="top">
    <td height="58" bgcolor="#990000">
      <div align="center"><font color="#000066"><b><font color="#FFFFFF"><br>
        <br>
      </font></b></font></div>
    </td>
  </tr>
</table>
<script type="text/javascript">
            <!--
            if (document.loginform) {
               document.loginform.login.focus();
            }
            // -->
            </script>
            </body>
            </html>
            <?
      } 

// --------------------------------------------------------------------------------------
// main
// --------------------------------------------------------------------------------------

      //init vars;
      $mysession = array ("login"=>FALSE, "passwd"=>FALSE, "ID"=>FALSE, "valid"=>FALSE); 
      $uri = basename($PHP_SELF); 
      $stamp = md5(srand(5)); 

      if(!auth()):                 // authentication failed
        $success=0;
          //    echo "-----".$USNAME;
              if($USNAME):
                $db=db_connection2("mydtbase");
                db_update2("UPDATE admin SET session_id='' WHERE idpengguna='$USNAME'"); //delete user session_id
            endif;
        LoginForm();               // display login form

      else:                        // authentication was successful
      $success=1;
      $tmp = session_id();         // session is already started

      $USER[NAME]=$mysession["login"];
      $USNAME=$mysession["login"];
  //is it the proper syntax to redirect to restricted page after success login
      header("location:restricted.php");
      endif; 

?>

ok for restricted page I just put this on top of the page…



<?
include_once "login.php";
    if($success==1){
?>
//the whole html stuff goes here then at the bottom I close the if condition

<?  }//end if  ?>


anyway thanks for the concern…and ur times