SmItH197 / SteamAuthentication

A simple PHP Authentication that enables steam users to log into their steam account to access content!
MIT License
445 stars 145 forks source link

Undefined variable and MySQL #52

Closed 1sirel closed 9 years ago

1sirel commented 9 years ago

I get these errors in my log whenever I try and use PHP to submit to the DB

Errors:

  PHP Notice:  Undefined variable: _SESSION in /home11/example/public_html/steamauth/userInfo.php on line 4
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 6
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 7
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 8
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 9
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 10
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 11
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 12
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 13
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 14
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 15
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 21
  PHP Notice:  Undefined offset: 0 in /home11/example/public_html/steamauth/userInfo.php on line 22

PHP code:

  <?php
  include ('steamauth/userInfo.php');

  $servername = "myserver";
  $username = "myname";
  $password = "mypass";
  $dbname = "mydb";

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

  $sql="INSERT INTO enterw (weekly) VALUES ('" . $steamprofile['steamid'] . "')";

  if ($conn->query($sql) === TRUE) {
  echo "Success";
  } else {
  echo "Error: " . $sql . "<br>" . $conn->error;
  }

  $conn->close();

  ?>

As a result I only get 0 submitted to the DB, how would I go about fixing this?

SmItH197 commented 9 years ago

Can you post your user info.php?

1sirel commented 9 years ago

Sorry fell asleep here is my UserInfo

    <?php
include("settings.php");
if (empty($_SESSION['steam_uptodate']) or $_SESSION['steam_uptodate'] == false or empty($_SESSION['steam_personaname'])) {
    $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'] = true;
}

$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'];
?>
SmItH197 commented 9 years ago

Put session_start(); at the top of your file before include ('steamauth/userInfo.php');.

http://stackoverflow.com/questions/15594498/notice-undefined-variable-session-in-on-line-9

1sirel commented 9 years ago

Cool it is all working. :) Thanks!

Didn't think it would be so simple. Do you know why the login and test pages still work even though they don't have session_start();?

SmItH197 commented 9 years ago

The demo and test pages work because they include steamauth.php not settings.php. The session is started inside the steamauth.php file whereas it isn't in settings.php.

Compare the first three lines of code in settings.php and steamauth.php

1sirel commented 9 years ago

Sorry I am a derp. Thanks for helping me though :)

Also thanks for building this I never would have been able to get steamlogins working without it

SmItH197 commented 9 years ago

No problem, glad I could help :)