在MYSQL中保存变量的问题

时间:2017-07-28 19:10:03

标签: php mysql mysqli steam steam-web-api

我目前正在建立一个网站,用户可以使用他的Steam帐户登录。为此,我使用的是Steam身份验证库,您可以在此处找到:https://github.com/SmItH197/SteamAuthentication

我想要做的是在用户登录我的网站时将ID64,名称和链接保存到我的MYSQL数据库中的用户头像。那些东西都包含在变量$ steamid64,$ steamname& $ steamavatar。我写了以下脚本来实现这个目标:

<?php
require '../steamauth/steamauth.php';

if(isset($_SESSION['steamid'])) {

    include_once 'connect.php';
    include_once '../steamauth/userInfo.php';

    $steamid64 = mysqli_real_escape_string($conn, $_SESSION['steam_steamid']);
    $steamname = mysqli_real_escape_string($conn, $_SESSION['steam_personaname']);
    $steamavatar = mysqli_real_escape_string($conn, $_SESSION['steam_avatar']);

    //Error handlers
    //Check for empty fields
    if(empty($steamid64) || empty($steamname) || empty($steamavatar)) {
        header("Location: ../?userdata=empty");
        exit();
    } else {
        //Check if input characters are valid
        if (!preg_match("/^[0-9]*$/", $steamid64)) {
            header("Location: ../?userdata=invalid");
            exit();
        } else {
            //Insert USERDATA into database
            $sql = "INSERT INTO `users` (`steamid64`, `name`, `avatar`) VALUES ('$steamid64', '$steamname', '$steamavatar')";
            mysqli_query($conn, $sql);
            header("Location: ../?userdata=saved");
            exit();
        }
    }

} else {
    header("Location: ../");
    exit();
}

?>

脚本检查用户是否通过“if(isset($ _ SESSION ['steamid]))”登录,然后继续执行一些错误处理程序,然后向我的数据库发送查询,插入其中的信息我的变数。

我的问题是我的脚本不起作用,我不确定到底发生了什么(这就是我要问的原因)但是它不会在我的数据库中创建一个新条目。

您可以在下面找到steamauth.php,connect.php和userInfo.php。我希望这足以解决我的问题。如果您需要更多信息,请在下面发表评论,我很乐意提供。谢谢你的努力!

steamauth.php:https://github.com/SmItH197/SteamAuthentication/blob/master/steamauth/steamauth.php

userInfo.php:

<?php
if (empty($_SESSION['steam_uptodate']) or empty($_SESSION['steam_personaname'])) {
    require 'SteamConfig.php';
    $url = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=".$_SESSION['steamid']); 
    $content = json_decode($url, true);
    $_SESSION['steam_steamid'] = $content['response']['players'][0]['steamid'];
    $_SESSION['steam_communityvisibilitystate'] = $content['response']['players'][0]['communityvisibilitystate'];
    $_SESSION['steam_profilestate'] = $content['response']['players'][0]['profilestate'];
    $_SESSION['steam_personaname'] = $content['response']['players'][0]['personaname'];
    $_SESSION['steam_lastlogoff'] = $content['response']['players'][0]['lastlogoff'];
    $_SESSION['steam_profileurl'] = $content['response']['players'][0]['profileurl'];
    $_SESSION['steam_avatar'] = $content['response']['players'][0]['avatar'];
    $_SESSION['steam_avatarmedium'] = $content['response']['players'][0]['avatarmedium'];
    $_SESSION['steam_avatarfull'] = $content['response']['players'][0]['avatarfull'];
    $_SESSION['steam_personastate'] = $content['response']['players'][0]['personastate'];
    if (isset($content['response']['players'][0]['realname'])) { 
           $_SESSION['steam_realname'] = $content['response']['players'][0]['realname'];
       } else {
           $_SESSION['steam_realname'] = "Real name not given";
    }
    $_SESSION['steam_primaryclanid'] = $content['response']['players'][0]['primaryclanid'];
    $_SESSION['steam_timecreated'] = $content['response']['players'][0]['timecreated'];
    $_SESSION['steam_uptodate'] = time();
}
$steamprofile['steamid'] = $_SESSION['steam_steamid'];
$steamprofile['communityvisibilitystate'] = $_SESSION['steam_communityvisibilitystate'];
$steamprofile['profilestate'] = $_SESSION['steam_profilestate'];
$steamprofile['personaname'] = $_SESSION['steam_personaname'];
$steamprofile['lastlogoff'] = $_SESSION['steam_lastlogoff'];
$steamprofile['profileurl'] = $_SESSION['steam_profileurl'];
$steamprofile['avatar'] = $_SESSION['steam_avatar'];
$steamprofile['avatarmedium'] = $_SESSION['steam_avatarmedium'];
$steamprofile['avatarfull'] = $_SESSION['steam_avatarfull'];
$steamprofile['personastate'] = $_SESSION['steam_personastate'];
$steamprofile['realname'] = $_SESSION['steam_realname'];
$steamprofile['primaryclanid'] = $_SESSION['steam_primaryclanid'];
$steamprofile['timecreated'] = $_SESSION['steam_timecreated'];
$steamprofile['uptodate'] = $_SESSION['steam_uptodate'];
// Version 3.2
?>

connect.php(mysql的连接设置):

<?php
$dbServername = "removed";
$dbUsername = "removed";
$dbPassword = "removed";
$dbName = "removed";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

0 个答案:

没有答案
相关问题