简体   繁体   中英

Do I really have to reload my page twice?

I am currently working on a project including the Facebook SDK. I already made it to Login/Logout myself using the javascript code.

I'm using an AJAX POST request to save the userID and the name from the response in a Session.

The problem here is, that I actually have to reload the page twice, the first time to get the POST parameters and save them into a $_SESSION. The second refresh is needed to load the Session.

Is there a clean way to avoid that?

javascript: $.post( "login.php", { id:userID, name:response.name } );

login.php: $_SESSION['name'] = $_POST['name']; $_SESSION['userID'] = $_POST['id'];

I appreciate every kind of help. Thank you.

edit: I would like to give the user who logged in with facebook additional oppurtunities on my website. The only way I know how to do this is with a Session in PHP. Whenever he logged in I created a Session who said that a person is logged in.

Now I have to do the same with a facebook login. It worked local with the PHP SDK already, but the webspace does not support that kind of SDK. That is why I have to dodge to the javascript one. Is there another way to make sure a person is logged?

You are using AJAX,
So you can avoid reloading of Page,

<?php    
    session_start();

    if (isset($_GET['name'])) {$_SESSION['name'] = $_GET['name'];}
    if (isset($_GET['userID'])) {$_SESSION['userID'] = $_GET['userID'];}

    if(isset ($_POST['name']  ) && isset ($_POST['userID'])){
        $_SESSION['name']= $_POST['name']; 
        $_SESSION['userID']=  $_POST['userID'];
    }else{
        $_SESSION['userID'] = 0;
    } 
?>

and javascript code is:

$.post( "/login.php", { id:userID, name:response.name } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM