1

I am working on a web site with login registration functionality.I have implemented the login registration part with reference and forum help,but now I am facing problem with the PHP session.This is my ajax form submit:

   $.ajax({
                    type: "POST",
                    url: "userLogin.php",
                    data: "eid="+eid+"&pwd="+pwd+"&rememberme="+remember,
                    dataType: "html",
                    success: function(msg){
                        if($.trim(msg)!="error")
                        {
                            $("#user_status", window.parent.document).html("Welcome "+msg+"&nbsp;&nbsp;|&nbsp;&nbsp;<a href='forum/logout.php'>Logout</a>");

                            if(window.parent.document.getElementById('post_user_name'))
                                $("#post_user_name", window.parent.document).html("<strong style='color:#333333;'>"+msg+"</strong>");

                            parent.$.fancybox.close();
                        }
                        else{
                            $('#error_common').html("Invalid ID or password.Please try again");
                            $('#error_common').show();
                            return false;
                        }
                    }
                });

This is the PHP code:

             if(isset($_POST['eid']) && isset($_POST['pwd']))
          {     
                $userObj = new Model_Users();
                $username= return_fetched_value($_POST['eid']);
                $password= md5(return_fetched_value($_POST['pwd']));
                $getUserDetails = $userObj->getUserValueByUserNameAndPassword($username,$password);
                /*print_r($getUserDetails);
                exit;*/

                if(count($getUserDetails)>0)
                {   

                    $_SESSION['username'] = $getUserDetails['uname'];
                    $displayUser=$getUserDetails['uname'];  
                    echo $displayUser;

                }
                else
                {
                    //echo "entered here";  
                    echo "error";
                }

          }
          else
          {
            $errorMessage = "You have entered invald username password" ;

          }

Everything works fine,the user logs in and get the username on the login page as here http://www.proptiger.com/,but if I refresh the page the username disappears,but it is still present in the session.I can see it if I do print_r($_SESSION). The username has been set using the jquery ajax portion.

        $("#user_status", window.parent.document).html("Welcome "+msg+"&nbsp;&nbsp;|&nbsp;&nbsp;<a href='forum/logout.php'>Logout</a>");

Which I think is not persisted,but still there in the session.I am new to jquery ajax and stuck at this.Any constructive comments would be helpful. Thanks

1 Answer 1

1

This might sound silly but are you sure you're displaying the username with php on page load or is it just when there's a login ajax call?

As far as I can tell your code only displays the username after your AJAX call, but there isn't anything like <?php echo $_SESSION['username']; ?> when you load the page without ajax.

I hope my answer is clear enought, but just to conclude: Make sure you echo the username when someone goes directly into your website and not just on the login action.

Sign up to request clarification or add additional context in comments.

10 Comments

Yes, but how to overcome it?I can do it using PHP ,but then using jquery/ajax for submition won't be of much use? Currently I am not having multiple pages,but yes that has to be tested if user goes directly to website.I have added a link above to the site having similar stuff,how is it exactly done?can you throw some light on it?Any pointers or examples probably.
Are you using php files to render the HTML?
That's not a problem. Just in your <somepage>.php where you want the username to appear add <?php echo $_SESSION['username']; ?>
That is one obvious way.But I wonder how the mentioned website has implemented it and my requirement says something similar to be done.Can't we persist those variables in ajax?I understand one is client side and other server side(kind of).I tried using <?php echo $_SESSION['username']; ?> in ajax call but doesn't work(obvious i think).For now may be I will got with your previous comment till I have some other. Thanks for the time
Hard to tell how exactly they've done it, but a simple answer would be yes :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.