Search code examples
phpmysqltwitteroauthtwitter-oauth

sync two table user_id and thanks confirmation to user with twitter oauth


So this is a type of follow up issue from the question i asked yesterday, which was solved.

But now I have another issue. I have been trying to get the user_id from 2 different tables in the same database linked, so when a user authenticates their twitter it then displays a inner page that says *You have already linked your twitter! "username" so the 2 table layouts and code from all the pages is below:

users table: this table is for twitter oauth

ID, user_id, oauth_provider, oauth_uid, username, fname, lname, locale, oauth_token, oauth_secret, picture, created, modified

points table: this table is for the points and youtube stuff, (this table user_id already gets the data from my phpBB database from phpbb_users table.)

user_id, username, yt_channelTitle, points, user_email, access_token, refresh_token, channel_id, uploads, dailyVotes

twitter oauth functions.php file:

function checkUsers($user_id,$oauth_provider,$oauth_uid,$username,$fname,$lname,$locale,$oauth_token,$oauth_secret,$profile_image_url){
    $prevQuery = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
    if(mysqli_num_rows($prevQuery) > 0){
        $update = mysqli_query($this->connect,"UPDATE $this->tableName SET  oauth_token = '".$oauth_token."', oauth_secret = '".$oauth_secret."', modified = '".date("Y-m-d H:i:s")."', user_id = '".$user_id."' WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."' AND user_id = '".$user_id."' ") or die(mysqli_error($this->connect));
    }else{
        $insert = mysqli_query($this->connect,"INSERT INTO $this->tableName SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$oauth_uid."', username = '".$username."', fname = '".$fname."', lname = '".$lname."', locale = '".$locale."', oauth_token = '".$oauth_token."', oauth_secret = '".$oauth_secret."', picture = '".$profile_image_url."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."', user_id = '".$user_id."' ") or die(mysqli_error($this->connect));
    }

    $query = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
    $result = mysqli_fetch_array($query);
    return $result;
}

twitteroauth process.php file:

    include_once("../inc/header_session.php");
#$user_id = getUserInfo($id,$user->data['user_id']);
$user_id = intval($user->data['user_id']);

if($connection->http_code == '200' && $user_id != 1)
{
    //Redirect user to twitter
    $_SESSION['status'] = 'verified';
    $_SESSION['request_vars'] = $access_token;

    //Insert user into the database
    $user_info = $connection->get('account/verify_credentials'); 
    $user_id = $user->data['user_id'];
    $name = explode(" ",$user_info->name);
    $fname = isset($name[0])?$name[0]:'';
    $lname = isset($name[1])?$name[1]:'';
    $db_user = new Users();
    $db_user->checkUsers($user_info->user_id,'twitter',$user_info->id,$user_info->screen_name,$fname,$lname,$user_info->lang,$access_token['oauth_token'],$access_token['oauth_token_secret'],$user_info->profile_image_url,$user_info);

    //Unset no longer needed request tokens
    unset($_SESSION['token']);
    unset($_SESSION['token_secret']);
    header('Location: /twitter.php');
}else{
    die("error, try again later!");
}

and now the twitter.php page https://www.tubenations.com/twitter.php

if ($user_login == 1){
echo '<h1 class="icon fa-twitter"> Twitter Page!</h1>';
    $user_id = intval($user->data['user_id']);
    include "twitter/includes/functions.php";
    #$user_info = checkUsers($user_id,$oauth_provider,$oauth_uid,$username,$fname,$lname,$locale,$oauth_token,$oauth_secret,$profile_image_url);
    echo $oauth_uid;

    if (empty($user_info['oauth_uid'] == 1))
        {
            echo '
            <div class="row">
                    <div class="8u">                    
                        <br />
                        <div class="twitterInfo2">IMPORTANT ANNOUNCEMENT: We are currently trying to setup a twitter authentication system, so that users have to authenticate their twitter accounts 
                        in order for them to be displayed on this page. So please bear with us! <br /></div>
                        On this page you can find all of our users Twitter accounts in one useful page, feel free to follow who you like the look off.<br />
                        We made this page as an extra feature, so it makes it easier to follow people!<br />
                        <strong>'.$user_info['oauth_uid'].'</strong>
                        <hr>
                        <div class="twitterInfo2">In order for you to appear on this page you need to authenticate your twitter account by clicking below! <br /></div>
                        <a href="/twitter/process.php"><img src="/twitter/images/sign-in-with-twitter.png" width="151" height="24" border="0" /></a>
                        <hr>

                    </div>
                        <div class="4u">

                        <a class="twitter-timeline" data-dnt="true" href="https://twitter.com/TubeNations" data-widget-id="635542971107143680">Tweets by @TubeNations</a>

                        </div>
            </div>';
        }
            else
            {
                echo '
            <div class="row">
                    <div class="8u">                    
                        <br />
                        <div class="twitterInfo2">IMPORTANT ANNOUNCEMENT: We are currently trying to setup a twitter authentication system, so that users have to authenticate their twitter accounts 
                        in order for them to be displayed on this page. So please bear with us! <br /></div>
                        On this page you can find all of our users Twitter accounts in one useful page, feel free to follow who you like the look off.<br />
                        We made this page as an extra feature, so it makes it easier to follow people!<br />
                        <hr>
                        <div class="twitterInfo2">You have already linked your twitter! <strong>'.$user_info['oauth_uid'].'</strong><br /> </div>
                        <hr>';
            echo getTwitterInfo();          
            echo '      </div>
                        <div class="4u">

                        <a class="twitter-timeline" data-dnt="true" href="https://twitter.com/TubeNations" data-widget-id="635542971107143680">Tweets by @TubeNations</a>

                    </div>
            </div>';
            }

            echo '</div>';
        }

I have tried many different combinations and failing badly... if i am missing any more code or info you need let me know


Solution

  • I managed to work it out myself. so I thought I would post my own answer :)

    So i basically used a variable from other functions and also used this below in the process.php file.

    user->data['user_id'] 
    

    I then moved my user_id row to the end in my sql users table, and I also added all of the extra SQL inserts at the end of each line in the functions.php file

    user_id = '".$tn_userid."'
    

    Then to fix the issue with the twitter page, I made a function to fetch the data from the users table:

    function getUserTwitterInfo($id){
    $mysqli = db_connect();
    $query = "SELECT * FROM `**HIDDEN**`.`users` WHERE `user_id`= ? ";
    
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $id = mysqli_real_escape_string($mysqli,$id);
    if ($stmt = $mysqli->prepare($query)){
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($myrow = $result->fetch_assoc()) {
            $user_info = $myrow;
        }
        $stmt->close();
    }
    
    $mysqli->close();
    return $user_info;
    

    then i just declared it in the twitter page:

    if ($user_login == 1){
    echo '<h1 class="icon fa-twitter"> Twitter Page!</h1>';
        #$user_id = intval($user->data['user_id']);
        include "twitter/includes/functions.php";
        $id = $phpbb_user_id;
        $user_info = getUserTwitterInfo($id);
    
        #echo $oauth_uid;
    
        if ($user_info['user_id'] == 0)
            {
                echo '
    

    I am so happy now that I managed to do it :) bit of a shame I didnt get a answer posted though :(