如果他们直接访问它,直接PHP文件错误消息

时间:2013-07-15 12:29:55

标签: php

我有一个名为“Status.php”的php文件,如果他们使用正确的URL,基本上允许该人

http://konvictgaming.com/status.php?channel=isypie提取他们的twitch.tv流在线/离线状态,完美无缺

但我想这样,如果他们试图直接访问status.php文件 http://konvictgaming.com/status.php 它将返回错误

错误:请在您的链接中添加一个流用户。 例如:http://konvictgaming.com/status.php?channel=konvictgaming

我当前的代码

<?php

$channelName = htmlspecialchars($_GET['channel'], ENT_QUOTES);

$clientId = '';             // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png';     // Set online image here
$offline = 'offline.png';   // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);

header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
    $channelTitle = $json_array['stream']['channel']['display_name'];
    $streamTitle = $json_array['stream']['channel']['status'];
    $currentGame = $json_array['stream']['channel']['game'];

    $image = file_get_contents($online);
} else {
    $image = file_get_contents($offline);        
}
echo $image;

?>

1 个答案:

答案 0 :(得分:2)

只需在脚本的开头添加一个检查:

if(empty($_GET['channel']))
    die('ERROR: Please put a stream user in your link. ex: http://konvictgaming.com/status.php?channel=konvictgaming');