douguan3470 2015-07-21 18:58
浏览 65

PHP在数组内部回显$ config

Alright so I've got a config file that looks like this:

<?php
# EPICMC CMS CONFIG FILE 

return array(
    'sitename' => 'EPICMC',
    'color' => '00aa00',
    'url' => 'stats.epicmc.us',
    'protocol' => 'https://',
);

And here's my webpage.

<head>
       <link rel="icon" 
          type="image/png" 
          href="avatar/<?php                     
             $statsJSON = file_get_contents($configs['url']/api.php?task=stats&player='.$_GET['player'].'');
              $stats = json_decode($statsJSON);
              if(empty($stats)){
              echo $_GET['player'];
              echo '/16';
              } else {
                           echo strtoupper($stats->skin); 
                           echo '/16';
             }
             ?>">
       <link rel="apple-touch-icon" 
          type="image/png" 
          href="avatar/<?php                     
             if(empty($stats)){
             echo $_GET['player'];
             echo '/250';
             } else {
                          echo strtoupper($stats->skin); 
                          echo '/250';
             }
             ?>">
       <title><?php echo "$configs['sitename']' - "; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?></title>
       <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?>">
       <meta name=viewport content="width=device-width, user-scalable=no">
       <meta name="description" content="View <?php echo $_GET['player'];?>'s <?php echo $configs['sitename']?> stats!">
       <style>#profile::before{display:block;height:144px;border-radius:0;background:#0a0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02);content:''}#profile{background:#fafafa;box-shadow:rgba(0,0,0,.14902) 0 1px 1px 0,rgba(0,0,0,.09804) 0 1px 2px 0;padding-bottom:38px;text-align:center;max-width:80%}#profile>img{position:relative;top:-52px;display:block;margin:auto;width:96px;height:96px;border:4px solid #fff;border-radius:0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02)}#profile h2,#profile h3{font-weight:400;color:#77767e;margin-bottom:30px}#profile h2{margin-top:-30px;padding:0 16px 38px;font-size:21px}#profile h3{margin-top:-60px;padding:0 38px 16px;font-size:11px}#profile h4{font-weight:400}#locked{margin-bottom:-80px}table,td,th{border:1px solid #000;border-collapse:collapse;width:80%}td,th{padding:5px;text-align:left}</style>
    </head>
    <body>
       <?php include("layout/header.php"); ?>
       <BR>
       <BR>
       <center>
          <aside id="profile">
             <?php
                function mcus_timeAgoString($dateInterval) {
                $daysAgo = $dateInterval->days;
                //echo $difference;
                if ($daysAgo == 0){
                if ($dateInterval->h == 0){
                  $diffStr = $dateInterval->i . " Minutes Ago";
                } else {
                   $diffStr = $dateInterval->h . " Hours Ago";
                }
                } else {
                if($daysAgo === 1) {
                  $diffStr = "1 Day Ago";
                } else if($daysAgo === 7) {
                  $diffStr = "1 Week Ago";
                } else if($daysAgo === 14) {
                  $diffStr = "2 Weeks Ago";
                } else {
                  $diffStr = "$daysAgo Days Ago";
                }
                }
                return $diffStr;
                }

                      $get_player = $_GET['player'];

                      $infoJSON = file_get_contents('$url/api.php?task=info&player='.$_GET['player'].'');
                      $info = json_decode($infoJSON);
                      $statsJSON = file_get_contents('$url/api.php?task=stats&player='.$_GET['player'].'');
                     $stats = json_decode($statsJSON);
                      // $now = time(); // or your date as well`enter code here`
                ?>
             <img src="avatar/<?php                     
                if(empty($stats)){
                echo $_GET['player'];
                echo '/96';
                } else {
                             echo strtoupper($stats->skin); 
                             echo '/96';
                }
                ?>">
             <?php 
                echo '<style>';
                echo '#verified {';
                echo 'fill:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '.toggle {';
                echo 'background-color:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo ' !important }';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background-image:';
                echo 'url(';
                echo substr($info->cover, 0, 50); 
                echo ' ) !important;';
                echo '</style>';
                ?>
             <h2>
                <?php
                   if($info->locked == 1){
                             echo '<div id="locked"><h5>This username is locked.</h5></div>';
                       }else {
                       echo strip_tags($_GET['player']);
                       }
                       ?>
                <?php
                   if(substr($info->verified,0,2) == 1) {
                         echo '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="verified" width="24" height="24" viewBox="0 0 24 24">
                   <path d="M10,17L5,12L6.41,10.58L10,14.17L17.59,6.58L19,8M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3Z" />
                </svg>
                '
                ;
                }
                //print_r ($stats);
                if(substr($info->locked,0,2) == 0) {
                echo '
                <h3>
                ';
                echo 'Logged In ';
                if(empty($info)){
                echo "Not Registered";
                } else { 
                //echo substr($playerAccountInfo->lastlogin,0,50); 
                $dtNow = new DateTime('Now');       
                $dtLastLogin = new DateTime($info->lastlogin);
                $diLastLoginToNow = $dtNow->diff($dtLastLogin);
                echo mcus_timeAgoString($diLastLoginToNow);
                }
                echo '';
                ;
                }
                ?>
             </h2>
             <center>
                <table>
                   <tr>
                      <th>Kills</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Deaths</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Ratio</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->ratio, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>
                <table>
                   <tr>
                      <th>Joins</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->joins, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Quits</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->quits, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Kicked</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kicked, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>    
                <table>
                   <tr>
                      <th>Places</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->places, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Breaks</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->breaks, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Chats</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->chats, 0, 50); ?></td>
                   </tr>
                </table>
                </div>
             </center>
             <BR>
       </center>
       </aside>
       </center>
       <center>
          <form class="search" action ="stats.php" method="GET"><input type="text" name="player" autocomplete="off" placeholder="Player Name"><BR><input type="submit" value="Search Stats"></form>
       </center>
       <BR>
       <BR>
       <?php include("layout/footer.php"); ?>
    </body>
    </html>

It let's me echo them in my header.php which is where I included the config by using

 <?php $configs = include('config.php'); ?>

And in my header.php it let me echo things like sitename using:

 <?php echo $configs['sitename']?>

, but my webpage isn't working. Keep in mind the header.php is included (which has the config.php included in it). I'm a PHP newbie, and I'm pretty sure I just echoed them wrong or something. I'm trying to get $sitename, $url, and $color to be dynamic.

A working version:

<?php $configs = include('config.php');
if($_GET['player'] === 0) header("Location: http://" + $configs['url']);
 ?>
<head>
       <link rel="icon" 
          type="image/png" 
          href="face.php?u=<?php                     
             $statsJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=stats&player='.$_GET['player']);
              $stats = json_decode($statsJSON);
              if(empty($stats)){
              echo $_GET['player'];
              echo '&s=16';
              } else {
                           echo strtoupper($stats->skin); 
                           echo '&s=16';
             }
             ?>">
       <link rel="apple-touch-icon" 
          type="image/png" 
          href="face.php?u=<?php                     
             if(empty($stats)){
             echo $_GET['player'];
             echo '&s=250';
             } else {
                          echo strtoupper($stats->skin); 
                          echo '&s=250';
             }
             ?>">
       <title><?php echo $configs['sitename'] . " - "; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?></title>
       <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?>">
       <meta name=viewport content="width=device-width, user-scalable=no">
       <meta name="description" content="View <?php echo $_GET['player'];?>'s <?php echo $configs['sitename']?> stats!">
       <style>#profile::before{display:block;height:144px;border-radius:0;background:#0a0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02);content:''}#profile{background:#fafafa;box-shadow:rgba(0,0,0,.14902) 0 1px 1px 0,rgba(0,0,0,.09804) 0 1px 2px 0;padding-bottom:38px;text-align:center;max-width:80%}#profile>img{position:relative;top:-52px;display:block;margin:auto;width:96px;height:96px;border:4px solid #fff;border-radius:0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02)}#profile h2,#profile h3{font-weight:400;color:#77767e;margin-bottom:30px}#profile h2{margin-top:-30px;padding:0 16px 38px;font-size:21px}#profile h3{margin-top:-60px;padding:0 38px 16px;font-size:11px}#profile h4{font-weight:400}#locked{margin-bottom:-80px}table,td,th{border:1px solid #000;border-collapse:collapse;width:80%}td,th{padding:5px;text-align:left}</style>
    </head>
    <body>
       <?php include("layout/header.php"); ?>
       <BR>
       <BR>
       <center>
          <aside id="profile">
             <?php
                function mcus_timeAgoString($dateInterval) {
                $daysAgo = $dateInterval->days;
                //echo $difference;
                if ($daysAgo == 0){
                if ($dateInterval->h == 0){
                  $diffStr = $dateInterval->i . " Minutes Ago";
                } else {
                   $diffStr = $dateInterval->h . " Hours Ago";
                }
                } else {
                if($daysAgo === 1) {
                  $diffStr = "1 Day Ago";
                } else if($daysAgo === 7) {
                  $diffStr = "1 Week Ago";
                } else if($daysAgo === 14) {
                  $diffStr = "2 Weeks Ago";
                } else {
                  $diffStr = "$daysAgo Days Ago";
                }
                }
                return $diffStr;
                }

                      $get_player = $_GET['player'];

                     $infoJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=info&player='.$_GET['player']);
                      $info = json_decode($infoJSON);
                      $statsJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=stats&player='.$_GET['player']);
                     $stats = json_decode($statsJSON);
                      // $now = time(); // or your date as well`enter code here`
                ?>
             <img src="face.php?u=<?php                     
                if(empty($stats)){
                echo $_GET['player'];
                echo '&s=96';
                } else {
                             echo strtoupper($stats->skin); 
                             echo '&s=96';
                }
                ?>">
             <?php 
                echo '<style>';
                echo '#verified {';
                echo 'fill:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";";
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '.toggle {';
                echo 'background-color:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo ' !important }';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background-image:';
                echo 'url(';
                echo substr($info->cover, 0, 50); 
                echo ' ) !important;';
                echo '</style>';
                ?>
             <h2>
                <?php
                   if($info->locked == 1){
                             echo '<div id="locked"><h5>This username is locked.</h5></div>';
                       }else {
                       echo strip_tags($_GET['player']);
                       }
                       ?>
                <?php
                   if(substr($info->verified,0,2) == 1) {
                         echo '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="verified" width="24" height="24" viewBox="0 0 24 24">
                   <path d="M10,17L5,12L6.41,10.58L10,14.17L17.59,6.58L19,8M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3Z" />
                </svg>
                '
                ;
                }
                //print_r ($stats);
                if(substr($info->locked,0,2) == 0) {
                echo '
                <h3>
                ';
                echo 'Logged In ';
                if(empty($info)){
                echo "Not Registered";
                } else { 
                //echo substr($playerAccountInfo->lastlogin,0,50); 
                $dtNow = new DateTime('Now');       
                $dtLastLogin = new DateTime($info->lastlogin);
                $diLastLoginToNow = $dtNow->diff($dtLastLogin);
                echo mcus_timeAgoString($diLastLoginToNow);
                }
                echo '';
                ;
                }
                ?>
             </h2>
             <center>
                <table>
                   <tr>
                      <th>Kills</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Deaths</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Ratio</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->ratio, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>
                <table>
                   <tr>
                      <th>Joins</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->joins, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Quits</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->quits, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Kicked</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kicked, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>    
                <table>
                   <tr>
                      <th>Places</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->places, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Breaks</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->breaks, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Chats</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->chats, 0, 50); ?></td>
                   </tr>
                </table>
                </div>
             </center>
             <BR>
       </center>
       </aside>
       </center>
       <center>
          <form class="search" action ="stats.php" method="GET"><input type="text" name="player" autocomplete="off" placeholder="Player Name"><BR><input type="submit" value="Search Stats"></form>
       </center>
       <BR>
       <BR>
       <?php include("layout/footer.php"); ?>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dongmo6937 2015-07-21 19:14
    关注

    You need to use curly braces around the array when using it quoted (or concatenate it).

    http://sandbox.onlinephpfunctions.com/code/28fb84721d5c91b0c48279163c967489923f150b

    So

    echo "$configs['sitename']' - ";
    

    Should be

    echo "{$configs['sitename']}' - ";
    

    or could be

    echo $configs['sitename'] . "' - ";
    

    Documentation (for the curly (complex) usage): http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

    A separate issue earlier in your code is

    $statsJSON = file_get_contents($configs['url']/api.php?task=stats&player='.$_GET['player'].'');
    

    you need to concatenate the arrays value with the query string

    $statsJSON = file_get_contents($configs['url'] . '/api.php?task=stats&player='.$_GET['player']);
    

    You should turn on error reporting to get all errors that your script is running into.

    How to get useful error messages in PHP?

    To enable and display errors add

    error_reporting(-1); // to enable all errors
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    

    don't do this on a production machine though, you don't want users encountering errors.

    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码