mysql表结构问题(用户登录和用户登录fb)

时间:2011-06-06 09:21:54

标签: php mysql

我有两个表,它们是我的登录/注册系统的一部分:

userlogin userlogin_fb

userlogin 处理传入的网站登录/注册

enter image description here

userlogin_fb 处理传入的Facebook连接登录/首次注册

enter image description here

更多详情@ http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

如果用户使用facebook连接登录 - 系统会检查他们是否已注册到数据库,如果没有,则为用户创建新记录:

if ($user){
// We have an active session, let's check if we have already registered the user
$query = mysql_query("SELECT * FROM userlogin_fb WHERE oauth_provider = 'facebook' AND oauth_uid = ".$user_profile['id']);
$result = mysql_fetch_array($query);
// If not, let's add it to the database
if(empty($result)){
    $query = mysql_query("INSERT INTO userlogin_fb (oauth_provider, oauth_uid, username, email) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['username']}', '{$user_profile['email']}')");  
    }
        } elseif(isset($_SESSION['uid'])){
"";
}else{
   // Anonymous user
header('Location: index.php');
}

在网站注册时,如果存在相同的用户名/电子邮件地址,则该用户无法注册(并创建重复记录)

//select all rows from our userlogin,userlogin_fb table where the emails match
$query = sprintf("SELECT 1 
                    FROM userlogin 
                   WHERE `email` = '%s'
                  UNION ALL
                  SELECT 1
                    FROM userlogin_fb 
                   WHERE `email` = '%s' ",
                   $email, $email);
$res1 = mysql_query($query);
$num1 = mysql_num_rows($res1);
//if the number of matches is 1
if($num1 >= 1) {
  //the email address supplied is taken so display error message
  echo '<p class="c7">The <b>e-mail</b> address you supplied is already taken. Please go <a href="register.php">back</a> and provide a new one.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
  include_once ("resources/php/footer.php");        
  exit; 
} else      

//select all rows from our userlogin,userlogin_fb table where the usernames match
$query2 = sprintf("SELECT 1 
                    FROM userlogin 
                   WHERE `username` = '%s'
                  UNION ALL
                  SELECT 1
                    FROM userlogin_fb 
                   WHERE `username` = '%s' ",
                   $username, $username);
$res2 = mysql_query($query2);
$num2 = mysql_num_rows($res2);
//if the number of matches is 1
if($num2 >= 1) {
  //the username supplied is taken so display error message
  echo '<p class="c7">The <b>username</b> you supplied is already taken. Please go <a href="register.php">back</a> and provide a new one.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
  include_once ("resources/php/footer.php");        
  exit;

现在我遇到的问题是这个。每当用户通过网站注册并使用facebook连接登录时,我发现无论在 userlogin_fb 表中是否创建了重复记录。

因为我不希望在两个表中有任何重复的用户名/电子邮件地址以及“两种不同的用户登录模式”,每种模式包含与用户有关的不同信息集,我如何集成这两种模式?

最好当用户使用facebook连接登录时,我会运行一个查询,检查 userlogin 表,看看是否首先存在重复的用户名并使用这些详细信息

如果 userlogin 中存在使用相同用户名/电子邮件的记录,则 userlogin_fb 中不会生成新记录。

但如果它们不存在,我可以为它创建一个新记录。

然后,如果用户尝试使用相同的用户名/电子邮件注册帐户,我会将其重定向到功能“合并”两个帐户。

但这只是一个粗略的想法。我希望进一步了解这个问题。

希望我措辞得足够好!

2 个答案:

答案 0 :(得分:2)

Facebook更新了他们的用户ID,你想要BIGINT(15),尽管如Sascha Galley所说,VARCHAR是未来的。

我有3个表:网站用户,Facebook用户和联合facebook网站用户。

数据独立存储在前两个表中,即相同的电子邮件可以在前两个表中出现一次,无关紧要,他们可以使用他们想要的任何一个登录。

如果serveur检测到user和fb用户,并且这些人希望关联这两个帐户,则在第三个表中创建一个新行,并在此表中合并帐户信息(帖子计数,评论......)。

答案 1 :(得分:1)

我也有类似的问题。您应该使用varchar(15)或bigint作为Facebook ID。

Is a facebook ID a string or an integer?

What should be the leagth of int in a sql table where i store the facebook profile id?

我会推荐varchar,因为它具有未来性,并且不会减慢你的系统速度。

相关问题