0

I have been searching for a solution and can't find one so the best place to ask is StackOverFlow.

I keep getting the error Undefined index: steamid(Line 54) The Error Is Coming From $steamid = $_SESSION["steamid"]; on the welcome page.

The session_start(); is defined in the header. This is the welcome page that users get re-directed to after logging in.

------------------ This is The Welcome page ------------------

<?php include('../header.php'); ?>
    <?php include('../footer.php'); ?>

    <?php
    $steamid = $_SESSION["steamid"];

    require('../int/dbinit.php');

    $sql = "SELECT steamid FROM users WHERE steamid='". $steamid ."'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        die("<!-- ACCOUNT ALREADY EXISTS! -->");
    }

    $sql = "INSERT INTO users (steamid) VALUES ('".$steamid."')";




    if ($conn->query($sql) === TRUE) {
      echo "<!-- ACCOUNT HAS BEEN CREATED! -->";
    } else {
      uhoh("Error while creating account: " . $sql . "<br>" . $conn->error);
    }

    ob_end_flush();

------------------ This is some of the Login Page ------------------

  foreach ($json_decoded->response->players as $player)
                {
                    /*echo "
                    <br/>Player ID: $player->steamid
                    <br/>Player Name: $player->personaname
                    <br/>Profile URL: $player->profileurl
                    <br/>SmallAvatar: <img src='$player->avatar'/> 
                    <br/>MediumAvatar: <img src='$player->avatarmedium'/> 
                    <br/>LargeAvatar: <img src='$player->avatarfull'/> 
                    ";*/
                    $_SESSION["steamid"] = $player->steamid;
                    $_SESSION["personaname"] = $player->personaname;
                    $_SESSION["profileurl"] = $player->profileurl;
                    $_SESSION["avatar"] = $player->avatar;
                    $_SESSION["avatar_medium"] = $player->avatarmedium;
                    $_SESSION["avatar_full"] = $player->avatarfull;
                    /*var_dump($player);exit;*/
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Tom Udding Sep 09 '17 at 07:06
  • Why are you setting all the session variables in a loop? You'll only get the values from the last element of the `players` array. – Barmar Sep 09 '17 at 07:23
  • And if the array is empty, you won't set any of the session variables. That's probably why it's undefined. – Barmar Sep 09 '17 at 07:24

1 Answers1

-2

Use isset() function to check whether it is defined or not.

$steamid = isset($_SESSION["steamid"]) ? $_SESSION["steamid"] : '';
HIR
  • 192
  • 3
  • 11
  • 1
    This doesn't really solve the problem, it just prevents the warning. He needs to figure out why the session variable isn't getting set in the first place. – Barmar Sep 09 '17 at 07:25
  • If you're getting undefined index errors, make sure that your indexes are set before you try to perform any task. See Documentation here : http://php.net/manual/en/function.isset.php – HIR Sep 09 '17 at 11:51
  • But isn't that the whole issue? He has a login page that's supposed to set all the session variables. Yet somehow they're not getting set. That's what needs to be fixed, not ignored with `isset()`. – Barmar Sep 12 '17 at 00:05