-1

Hii Everyone, Here is my code for facebook Signin.i got this example from http://www.codexworld.com/login-with-facebook-using-php/. here is my index page code

PHP code

<?php
include_once("config.php");
include_once("includes/functions.php");
//destroy facebook session if user clicks reset
if(!$fbuser){
    $fbuser = null;
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
    $output = '<a class="myLink" href="'.$loginUrl.'">Click Here To Proceed</a>';   
}else{
    $user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,birthday,picture');
    $user = new Users();
    $user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['birthday'],$user_profile['picture']['data']['url']);
    if(!empty($user_data)){
        $output = 'Thanks For Register With Spark.You Should Receive Confirmation Mail Shortly';

    }else{
        $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
    }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spark Login with Facebook</title>
<style type="text/css">
h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
</style>
</head>
<body>
<div>
<?php echo $output; ?>
</div>

</body>
</html>

Here i use a href to show facebook login.after te click of href it will redirecting to facebook login instead automatically want to click and it will redirect the page.Is there any possible way to do that.If anyone know the solution for the problem please help me!!

Kavya Shree
  • 1,014
  • 2
  • 17
  • 52
  • Even if you’re not going to do a server-side redirect as suggested, there would be no need to “click a link with jQuery”, but instead a simple assignment of the URL to `location.href` would achieve the same thing. – CBroe Feb 13 '16 at 14:17

2 Answers2

1

Why not do this with header location? Here´s how that works: PHP header(Location: ...): Force URL change in address bar

For example:

$loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $homeurl, 'scope' => $fbPermissions));
header("Location: " . $loginUrl);

Official docs: http://php.net/manual/function.header.php

No need for using an $output variable, or for creating a login page (if you want to auto-redirect anyway). Just redirect the user if he is not logged in yet. No need to do something shady like "auto-clicking the login button". It´s not a solution, it´s a bad workaround. Although, it would be a lot better if you would let the user click it. Redirecting to the login page without a proper intro page, where he can see what the App is about, is not a good idea.

Even better: Use the JavaScript SDK for login: http://www.devils-heaven.com/facebook-javascript-sdk-login/

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • Actually i have signin button in my website.so on click of that button it will redirect the above page but i dont want to show this simply i want to redirect to facebook page. – Kavya Shree Feb 13 '16 at 09:43
  • then check out header location. using javascript for auto-clicking is a really bad idea. you just want to redirect users who are not logged in to the login page, right? – andyrandy Feb 13 '16 at 09:44
  • In my code i specify homeurl severname everything in config.php file.i want to specify as variable only.. how it is possible in header. See my code there is $loginUrl and $output variable. Line no 7&8 – Kavya Shree Feb 13 '16 at 09:49
  • One issue. if i removed that $output while redirecting it s not working correctly there is again problem in redirecting – Kavya Shree Feb 13 '16 at 10:34
  • http://www.sparkoverseas.com/facebook_login?code=AQBn0ioo6jzXKKfPykPDGihGobGclSF03jqkS43zw7bKmJcntNB5VGOYBZLDUxqkENFC6raNJnai00V_iYqOsuxbo_g4UrTHvJyPbeZxALh2rfLRueY-joxq39l0HpuQyypKmPLmrDef2smYwD3IhIapRCNBHnzmAJum_A2KNtH8GQsdIQBMNaLhU-bcqCf11sc2gZ2A3-p7Bn3tLvj5Nwb-qaGX9GlejS92vCUQfjjWM_YdsbMjwnwNSc2LrzVIhcybH0XQaqnC2loIk7oecAQJHR3nS1eSjDWQcfXnpotGnf4iVF1XFfRqTRk6Pn0A6wkUcBB9fd_Tr6J5zTNnsiX1&state=af22b2a999b83ade3886bc7c7eba587a#_=_ error is coming like this – Kavya Shree Feb 13 '16 at 10:39
  • i don´t see any error, works fine for me. add a die() after the header call just in case. i hope you did not remove the $output variable in the other code too, where the user is logged in already? – andyrandy Feb 13 '16 at 10:40
0

If you want to change the webpage's URL, you do not need to use jQuery to click the link. Instead, there is a simpler way.

JS:

window.location = "http://www.example.com/";

By changing the window.location, you can "redirect" to a new URL.

If you want to redirect only after an event, you can put that in a function of yours.

Example:

window.location = "http://www.example.com"
Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63