我正在网站上工作,我对php和编程很新。我设法登录用户,我可以通过以下方式从我的网页中的任何位置访问我的用户数据:
if (logged_in() === true){
global $user_data;
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id,'username', 'id', 'password', 'first_name', 'last_name', 'email', 'imagename');
if (user_active($user_data['username']) === false) {
session_destroy();
header ('Location: logout.php' );
}
}
$errors = array();
?>
希望到目前为止很清楚。现在我还设法使用他的Facebook帐户注册我的用户。 fbfunctions.php:
require 'include.php';
function retrieve_fields($sf) {
return json_encode($sf);
}
function verify_fields($f,$sf) {
$fields = json_encode($sf);
return (strcmp($fields,$f) === 0);
}
function register_fbuser($resp) {
extract($resp["registration"],EXTR_PREFIX_ALL, "fb");
// prepare values
$fb_id = mysql_real_escape_string($resp["user_id"]);
$fb_birthday = date("Y-m-d" , strtotime($fb_birthday));
$fb_name = mysql_real_escape_string($fb_name);
$fb_first_name = mysql_real_escape_string($fb_first_name);
$fb_last_name = mysql_real_escape_string($fb_last_name);
$fb_username = mysql_real_escape_string($fb_username);
$fb_location = mysql_real_escape_string($fb_location["name"]);
$query_str = "INSERT INTO users(oauth_provider, oauth_uid, username, first_name, last_name, gender, birthday, location , email, imagename) VALUES ('facebook','$fb_id','$fb_name','$fb_first_name','$fb_last_name','$fb_gender', '$fb_birthday', '$fb_location','$fb_email','". $fb_id .".jpg')" or die(mysql_error());
mysql_query($query_str);
$imageData = file_get_contents("https://graph.facebook.com/" . $fb_id ."/picture");
file_put_contents("upload_img/" . $fb_id . ".jpg", $imageData);
}
function check_registration($fb, $fb_fields) {
if ($_REQUEST) {
$response = $fb->getSignedRequest();
if ($response && isset($response["registration_metadata"]["fields"])) {
$verified = verify_fields($response["registration_metadata"]["fields"], $fb_fields);
if (!$verified) { // fields don't match!
header("location: bad.php");
} else { // we verifieds the fields, insert the Data to the DB
$GLOBALS['congratulations'] = TRUE;
register_fbuser($response);
}
}
}
}
所以现在我在sql数据库中拥有所有用户数据,在文件夹中拥有用户图片。我想使用例如user_data['first_name']
而不是使用Facebook图形api user_profile['first_name']
来访问用户数据,因为我已经从Facebook获取了我需要的所有内容。
有没有办法验证用户是否登录Facebook然后设置会话,以便我可以简单地使用我的函数logged_in()
?
我知道这需要很多代码,你可能会在翻译中迷失方向,但我真的很困惑。