Hello, I have just recently completed a blog system with PHP. I am now
enhancing it with jQuery, but I have come across some problems.

My login form is in a toggle div, so I submit the form info
asynchronously to the db via jQuery and it's ajax features. However
when I do this the form does everything correctly, and reloads the
page...but for some reasons the sessions do not work, therefore you
are not logged in.

I have also noticed that a javascript reload will work with jQuery but
an html meta refresh will not, does anyone know why this is?

The code for my login form is this...

<?php

    echo "<form name='loginsub' method='post' action='db.php'>\n";
    echo "Username <input type='text' name='username' id='username'
autocomplete='off' />  \n";
    echo "Password <input type='password' name='password'
id='password' autocomplete='off' />  \n";
    echo "<input type='submit' value='Login' id='loginsubmit' />\n";
    echo "</form>\n";

?>

The code for db.php is this...

<?php

  @include('connect.php');

    $user = $_POST['username'];
    $pass = $_POST['password'];

    $pass = md5( $pass );
    $passm = sha1( $pass );
    $passn = md5( $passm );
    $passo = sha1( $passn );
    $passp = md5( $passo );

    if($user && $pass){
        $sql = "SELECT * FROM `users` WHERE `username`='$user'";
        $res = mysql_query($sql) or die(mysql_error());


          if(mysql_num_rows($res) == 1){
            $epass = md5($pass);
            $sql2 ="SELECT * FROM `users` WHERE `username`='$user' AND
`password`='$passp'";
            $res2 = mysql_query($sql2) or die(mysql_error());

              if(mysql_num_rows($res2) == 1){
              //successful login

                          echo "Currently logging you in, $user\n";
                          $row = mysql_fetch_assoc($res2);
              $_SESSION['uid'] = $row['id'];
                          echo "<script 
type='text/javascript'>setTimeout('location.reload
(true)',3000)</script>\n";
                          //echo "<meta http-equiv='refresh' content='4' />\n";

              }else {
    echo "<b>Username or password incorrect</b>\n";
              }
           }else {
    echo "<b>Username or password incorrect</b>\n";
           }
       }else {
    echo "<b>All fields need to be filled in</b>\n";
      }

?>

Then finally the jQuery for the form is this...

$(function(){
  $('#loginsubmit').click(function(){

    var username = $('#username').val();
    var password = $('#password').val();

    $.ajax({
          url: 'db.php',
          type: 'POST',
          data: 'username=' + username + '&password=' + password,

          success: function(result) {
              $('#response').remove();
                          $('#navloginf').fadeOut("slow");
              $('#nave').slideDown("fast");
              $('#nave').append('<p id="response">' + result + '</
p>');


                          }
    });

    return false;

});
});

If anyone could help me out on why the sessions are not work, that
would be very nice.

Reply via email to