0
votes

I have the following PHP code:

<php>
$servername = "host";
$username = "username";
$password = "password";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT steamid, bananas FROM es_player";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "PLAYER'S STEAM ID: " . $row["steamid"]. " - PLAYER'S BANANAS: " 
.$row["bananas"]. " <br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

It just fetches specific fields from my database. When user's login, they use OpenID, and it is not through an actual database under my control. It is through Steam, users login with their Steam account through OpenID. I am able to fetch the user's SteamID with this when they log in, and there is even a variable for it.

I need to use this SteamID variable when they are logged in to specify which row they are on the database, and print ONLY the logged in user's profile fields, rather than just printing all rows in the database. This will be done using the SteamID of the user that logs in which will be compared against the SteamID field on my database, so that it will know which user you are when you log in.

I do not know how to accomplish this, which is why I am posting here. I just need the PHP code, or some help writing it.

2
There's really no need to delete and repost your question, especially since it removes the comment history. I asked if you could be more specific about where you are stuck - "all of it" makes this, I think, too broad for Stack Overflow. If you can suggest one particular thing that you would like help on, that would be most helpful for readers.halfer
How are you reading a user's Steam ID? If you don't have any code for that, that is what you need to start on. I use lusitanian/oauth on GitHub to set up an app as an OpenID consumer, seems to work fine.halfer
See code examples here. It isn't trivial, unfortunately, for the beginner programmer. If you find the above a bit complicated, maybe search for "Steam OpenID PHP" - maybe there is some code you can use directly?halfer
Hang on, the library you mentioned in your original question may do just fine. Have you looked at the supplied demo?halfer
Well, my problem is that I am using this: github.com/SmItH197/SteamAuthentication but it does not give me their actual Steam Profile ID, it gives me their Steam Community ID (AKA Steam ID 64. I am not sure how to get the actual profile ID, which is what I have saved in my player database.Aris

2 Answers

0
votes

You have several related problems that need more research. Since I've voted to close the question as too broad, I will mark this answer as Community Wiki. I really want to help, but in common with the values of the community here, I would encourage you to take the following points and to use them as avenues for further search-engine research. Your post for "just [needing] the PHP code" is a request for free work, which we try to discourage here.

I think I understand the problem, but I have no experience of the Steam API, so you may need to read their docs and adapt the following. If you have not used APIs or sessions before, hiring a freelancer in your locality may be the quickest and easiest route to getting your project on the road. You may only need a few hours of their time, so it need not be expensive.

  1. Your OpenID script should deliver to your application one of the profile IDs you've described. When a user first creates an account in your site, you need to capture that profile ID and store it against other information of interest. At this point you should run the conversion routine, so that you have the other profile ID, and you can then store that too.
  2. When the user logs on, you need to create a session. This is usually as simple as using session_start() and then saving the user record primary key as a variable, thus:

    $_SESSION['user_id'] = $userId;
    

    The user ID will come from your login screen, where you get an OpenID callback to prove that the current user does indeed own the Steam profile ID they have supplied to you. Having a session set up means that any subsequent page browsed by the user will have their user ID available, until they log off. This means that you don't need to do an OpenID call on every page.

  3. Using this session ID, you can now obtain either of profile IDs you require, since they are both in your database. This is a trivial SELECT database operation involving the session ID, which you can read from $_SESSION['user_id].

Here is an example of a table in an OAuth application I wrote (it's open source, so you can pull it apart if you like). When the user logs on, this record is either created (if it does not exist) or updated (if it does exist):

mysql> SELECT * FROM user_auth;
+----+---------+---------------------------+----------+---------------------+
| id | user_id | username                  | provider | last_login_at       |
+----+---------+---------------------------+----------+---------------------+
|  1 |       1 | https://github.com/halfer | github   | 2015-01-13 18:05:49 |
+----+---------+---------------------------+----------+---------------------+
1 row in set (0.00 sec)

The username is the OpenID identifier, the provider is useful if you allow the user to choose from several authorisation systems (Steam being another), and of course the last_login_at is self-explanatory.


I also advised in the comments that you may have to write this code. Library re-use is a commendable habit to get into, but unfortunately there is not a library for every eventuality - sometimes the programmer just has to write something him or herself. We frequently see requests on Stack Overflow for a "library/tutorial/step-by-step guide for [project X]" here, and if readers can persuade posters that programming isn't really like that, they will have passed on a very useful lesson.

So, try the above in order? Feel free to ask for further help if I have misunderstood some basic part of the structure, but otherwise, please do use the keywords I've mentioned and pop them in a search engine. It's the only way to learn. Good luck!

0
votes

It was actually quite simple. First, I took the SteamID 64 variable $steamid and ran it through this conversion, which will output the SteamID 32

$id = $steamid;

function parseInt($string) {
    if(preg_match('/(\d+)/', $string, $array)) {
        return $array[1];
    } else {
        return 0;
    }}

$steamY = parseInt($id);
$steamY = $steamY - 76561197960265728;
$steamX = 0;

if ($steamY%2 == 1){
$steamX = 1;
} else {
$steamX = 0;
}

$steamY = (($steamY - $steamX) / 2);

And then to finish it off, I just made a variable that combined it into the full STEAM_0:000000000 combination.

$steamID = "STEAM_0:" . (string)$steamX . ":" . (string)$steamY;

I can now use $steamID as a variable on any page that I include this code on. I answered this myself so that if anybody else has troubles with this like I originally did, the answer will be here for them :)