0

Well, I am currently trying to write code which checks if a twitch stream is live or not. To do so I have created the array $_SESSION['errors']; On the second .php $_SESSION['errors'] is either given 'live' or 'Not live', however whenever it goes back to the first .php page the $_SESSION['errors'] will be overwritten at the point it was defined.

My question is, how, if possible would I get around this issue.

Code:

PHP in index.php:

<?php
$_SESSION['errors'] = '';
echo $_SESSION['errors'];
?>

PHP from streaminfo.php:

<?php
    if (empty($_POST) === false) {
        header('Location: index.php');
    }
    $streamer = $_POST['username'];
    $apiurl = "https://api.twitch.tv/kraken/streams/" . $streamer;
    $apicontent = file_get_contents($apiurl);
    $streamerinfo = json_decode($apicontent);

            if ($streamerinfo->stream == '') {
                echo '<h2>' . 'Streamer is not live' . '</h2>';
                $_SESSION['errors'] = 'not live';
                header('Location: index.php');
            }
?>

I realise that I am overwriting the array when index.php is loaded, however I must define it and I dont know what to define it with.

2
  • Where is your session_start(); ? Commented Apr 9, 2015 at 19:51
  • 2
    You have to define it? Why not check if it is defined and if it is not then you define it? Commented Apr 9, 2015 at 19:51

1 Answer 1

1

You can check if the element is empty, and if so, you can define it.

index.php:

<?php
session_start(); // Do you have this in another file?
if (empty($_SESSION['errors'])) {
    $_SESSION['errors'] = '';
}
echo $_SESSION['errors'];
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.