开发者

Seeing if I am Logged in on a different site PHP

开发者 https://www.devze.com 2023-03-13 15:36 出处:网络
On Domain.com I have a user handler that creates a session to tell him I am logged in. So on each page I simply just do:

On Domain.com I have a user handler that creates a session to tell him I am logged in.

So on each page I simply just do:

<?php 
require("http://www.domain.com/handlers/user_handler.php");
if ($user_data['loggedIn'] == 1){
echo "You are logged in!";};
?>

You can see if I am logged in I get a nice message that says you are logged in.

However I am wondering what it will take to tell on a different site if I am logged into the first site.

So for instance on Domain2.com. If I am logged into Domain.com can I have a message that displays You are logged into domain.com on do开发者_如何学Gomain2.com.

I know I wouldn;t just use the same code from above on the new domain but what is the best way to check if that session still going on from the main domain.com.

Let me know if I haven't clarified enough.

Thanks!


You could create a different script called, say, user_logged_in.php, which would take a user ID as parameter and would output 1 if that user is logged in or 0 otherwise. Then you can call that script from any domain.

Just make sure the call is secure. Maybe also include a key that must have a certain value in order for the script to respond, or check the IP of the caller to be that of domain2.com.

For example, you could access the script at a URL like:

http://www.domain.com/handlers/user_logged_in.php?id=48&key=SECRET_KEY

Then you would check in user_logged_in.php if $_REQUEST['key'] is indeed equal to SECRET_KEY (make that a little more secure though) and, if so, check the database to see if the user with id 48 is logged in or not.


Your script on Domain.com could look something like (pretend it's called login_check.php):

<?php
require("http://www.domain.com/handlers/user_handler.php");
if ($user_data['loggedIn'] == 1) echo "true";
else echo "false";
?>

Then, on Domain2.com, you would have a second script:

<?php
$loggedIn = file_get_contents("domain.com/login_check.php");
if ($loggedIn == "true") {
    // the user is logged in on Domain.com
} else {
    // the user is not logged in
}
?>

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号