在一个php文件中设置数组,并在另一个php文件中设置访问权限


Set array in one php file and access in another php file issue

我正在使用这段php代码编写带有填充变量的css。

我想将默认数组从该代码中移出,并将其提升到我的站点的settings.php文件中,但当我这样做时,我会收到以下错误消息:

<b>Warning</b>:  extract() [<a href='function.extract'>function.extract</a>]: First argument should be an array in <b>/home/drawapl1/public_html/ezamazon/demo/css/stylesheet.php</b> on line <b>19</b>

我的代码如下:

在index.php中:

<?php
include("settings.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="css/stylesheet.php" rel="stylesheet" type="text/css" media="screen" />

在stylesheet.php:中

/* get the stylesheet */
$stylesheet = @is_file($_GET['stylesheet']) && strtolower(substr(strrchr($file_name,'.'),1)) == 'css' ? $_GET['stylesheet'] : 'global.css';
/* set the header information */
//will be output as css
header('Content-type: text/css');
//set an expiration date
$days_to_cache = 10;
header('Expires: '.gmdate('D, d M Y H:i:s',time() + (60 * 60 * 24 * $days_to_cache)).' GMT');
//red css variable information
//$red = array(
  //'body_font_size' => '10px',
  //'body_text_color' => '#f00'
//);
/* extract the propery array's information */
extract($_GET['theme'] && ${$_GET['theme']} ? ${$_GET['theme']} : $defaultCSS);
/* load in the stylesheet */
$content = preg_replace('/'$(['w]+)/e','$0',@file_get_contents($stylesheet));
/* spit it out */
echo $content;

在设置中.pp:

$defaultCSS = array( 'body_background_colour' => 'white', 'body_text_colour' => 'black', 'body_font_size' => '1em', 'body_font_family' => 'Arial, Helvetica, sans-serif', 'h1_text_colour' => 'black', 'h2_text_colour' => 'black', 'h3_text_colour' => '#E47911', 'a_text_colour' => '#004B91', 'a_hover_text_colour' => '#2A70FC', 'news_block_background_colour' => '#EAF3FE', 'left_nav_border_colour' => '#C9E1F4', 'left_nav_h2_background_colour' => '#EAF3FE', 'button_text_colour' => '#FFFFFF', 'button_background_colour' => '#414953', 'button_hover_background_colour' => '#999999', 'price_colour' => '#990000', 'price_heading_colour' => '#666666', 'nav_links_colour' => 'black', 'nav_hover_links_colour' => '#E47911', 'hr_divider_colour' => '#DDDDDD', 'pagination_highlight_colour' => '#645538' );

知道问题是什么吗?

提前谢谢。

确保使用include()settings.php包含在所有内容之前。

这不起作用的最可能原因是文件不知道是否看到$default变量,并为参数创建了一个新的null变量。

试试这个:

//settings.php
  /* set the dynamic information */
  //default css variable information
  $default = array(
    'body_font_size' => '16px',
    'body_text_color' => '#00f'
  );
//main_php_file.php
  include('settings.php'); // This is the important part
  /* extract the propery array's information */
  extract($_GET['theme'] && ${$_GET['theme']} ? ${$_GET['theme']} : $default);

include('settings.php');之后再尝试print_r($default),以确保$default设置正确。

在index.php文件中包含settings.php,然后从html文档中调用stylesheet.php,将不会将settings.php中的变量传递给stylesheet.php

您需要在stylesheet.php中包含settings.php,以便能够在那里使用它的变量。

编辑

在index.php 中

<link href="css/stylesheet.php?vars=<?=urlencode(json_encode($defaultCSS))?>" rel="stylesheet" type="text/css" media="screen" />

在stylesheet.php中,在顶部添加

$defaultCSS = json_decode(urldecode($_GET['vars']));

您也可以使用serialize/unserialize而不是json_encode,但为什么不只包括它呢?将其保留在服务器端

会更干净