Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing value in session variable, and checking it

Tags:

javascript

php

i have made a login form on a light box using a javascript function. Now i want to store a value on session variable, so to check if the user has logined , and not to show him login lightbox again and again on his navigations on the page.. My javascript function is:

<script language="javascript" type="text/javascript">  
    function createlightbox()  
    {  
        document.getElementById('light').style.display='block';  
        document.getElementById('fade').style.display='block'  
    }  
    function closelightbox()  
    {  
        document.getElementById('light').style.display='none';  
        document.getElementById('fade').style.display='none'  
    } 
     function checksession()  
    {  if (admin=="admin")
    {closelightbox();}
    else
    {createlightbox();}  
    } 

function check(form)/*function to check userid & password*/
{
 /*the following code checkes whether the entered userid and password are matching*/
 if(form.name.value == "admin" && form.password.value == "admin")
  {
    closelightbox();
     var admin = <?php $_SESSION['Admin']= 1; ?>

  }
 else
 {
  document.getElementById("error").style.display='block';/*displays error message*/
  }
}      
</script>  

And i m calling the checksession function in my forms onsubmit event as

<form id="Admin" onreset="checksession()">

The problem is, on every reset or submit of form, even on the page changes, the login form is shown. Why it is not checking the check session function. Please tell me any fault i m making

like image 644
Hanya Idrees Avatar asked Nov 13 '22 06:11

Hanya Idrees


1 Answers

i'm not sure where your conditions are. but the following code should present in php script that generates your lightbox:

<?php echo '<script> var admin ='.$_SESSION['Admin'].'</script>'; ?>

(to check above is working correctly, you could View source code of your page and see if there is a line like: <script> var admin =1</script>)

the following should be before you access admin variable setted above:

<script language="javascript" type="text/javascript">  
.... //other code

 function checksession()  
    {  if(admin =="admin")
    {closelightbox();}
    else
    {createlightbox();}  
    } 
....

also note that if statement should compare == not assign =

like image 193
Elen Avatar answered Nov 16 '22 02:11

Elen