Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 359
Tarol Avatar asked Nov 09 '22 17:11

Tarol


1 Answers

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 } );
like image 172
Ashish Kamble Avatar answered Nov 14 '22 22:11

Ashish Kamble