0

我一直在开发一个网站(现在以这里为例:http : //neurotoxine.mine.nu/gloom),并且我从 styleswitcher 代码中得到了一个奇怪的行为。很大,请耐心等待。

首先,你可以先去我指定的地点看看。你有什么,它是一个 joomla 页面,使用 Jquery

var $j = jQuery.noConflict();

为了避免mootools冲突。然后,我使用了这些:

<?  // Checks for, and assigns cookie to local variable:
if(isset($_COOKIE['style']))
{   $style = $_COOKIE['style'];
}
// If no cookie is present then set style as "red" (default):
else {    $style = 'red';
}
?>

这只是cookie设置,如果cookie没有设置则$style=red,这个变量会附加到CSS中。像这样:

<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/wh1/css/template_<?php echo $style ?>.css" media="screen" />

第一个代码,$this->baseurl=directory 用于 joomla 安装,在本例中是阴暗的。第二个,echo $style,将写入 cookie 加载值或从选项分配的值,如果你第一次到达那里,那么你会得到 RED 作为值,template_red.css 将是 CSS整个网站。

有一个 JS,它可以做一些 jquery 技巧:

 jQuery.fn.styleSwitcher = function(){
  $j(this).click(function(){
      // We're passing this element object through to the loadStyleSheet function.
      loadStyleSheet(this);
      // And then we're returning false.
      return false;
  });
  function loadStyleSheet(obj) {
    $j('#preloader')
    // Now fade in the div (#preloader):
    .fadeIn(500,function(){
      // The following will happen when the div has finished fading in:
      // Request PHP script (obj.href) with appended "js" query string item:
      $j.get( obj.href+'&js',function(data){
        // Select link element in HEAD of document (#stylesheet) and change href attribute:
        $j('#stylesheet').attr('href','css/' + data + '.css');
        // Check if new CSS StyleSheet has loaded:
        cssDummy.check(function(){
          // When StyleSheet has loaded, fade out and remove the #overlay div:
          $j('#preloader').fadeOut(500);
        });
      });
    });
  }
  // CSS DUMMY SECTION
  var cssDummy = {
    init: function(){
      // Appends "dummy-element" div to body:
      $j('<div id="dummy-element" style="display:none" />').appendTo('body');
    },
    check: function(callback) {
      // Checks if computed width equals that which is defined in the StyleSheets (2px):
      if ($j('#dummy-element').width()==2) callback();
      // If it has not loaded yet then simple re-initiate this function every 200 milliseconds until it had loaded:
      else setTimeout(function(){cssDummy.check(callback)}, 200);
    }
  }
  cssDummy.init(); }

然后,我在我的页面中启动该功能:

$j(document).ready(function(){
$j('#style-switcher a').styleSwitcher();
});

我从这样的链接中调用它:

<a href="<?php echo $this->baseurl ?>/templates/wh1/style-switcher.php?style=fire">img</a>

最后,styleswitcher.php 代码在这里:

<?php
$style = $_GET['style'];
setcookie("style", $style, time()+604800*24); // 604800 = amount of seconds in one week *4=1 month *24=1/2 year *48=1 year
if(isset($_GET['js']))
echo $style;
else header("Location: ".$_SERVER["HTTP_REFERER"]); ?>

现在,问题是,当我在一个站点上测试它时,一切正常(http://neurotoxine.mine.nu/wht/index-test.php - stylechanger 的链接在它们的顶部和左侧说主题)。但是当我在 joomla 模板中插入整个代码时,整个系统都失败了。我在这里和那里做了一些修复(主要是 $j 声明),然后我意识到这可能与模板的路径有关,所以我测试了许多类型的声明、绝对路径、相对路径等,但没有任何反应。

然后我将 styleswitcher.php 复制到 joomla 的根目录 (neurotoxine.mine.nu/gloom) 并检查 ti 是否可以在那里工作,我得到一个空白页。我单击了返回,然后 WOW 样式转换器工作了,但是有些东西没有告诉返回到哪里,我认为它可能是标题(“位置:”.$_SERVER[“HTTP_REFERER”]); 指令,但我不知道要在那里改变什么才能使它工作。

Joomla 在其模板的标题中给出了一个声明:

<base href="http://neurotoxine.mine.nu/gloom/" />

我不知道这是否对 http_referer 的工作方式有影响,但我禁用了它,网站仍然这样做,单击样式,页面空白,点击返回,瞧,样式已更改但未能检索我所在的页面。

一些想法?任何帮助都可能有用。

4

2 回答 2

1

仅当您单击链接时(而不是直接在地址栏中输入时)设置引荐来源网址。事实上,浏览器会在用户点击此链接之前告诉网站用户去过哪里,因此您不能 100% 信任它(请参阅PHP 手册)。我会将上次访问的页面存储在 $_SESSION 中并重定向到这个页面;如果它为空,则重定向到 $this->baseurl 。'/index.php'。

base-Tag 仅用于相对链接,例如

<a href="index.php">

甚至:

<a>

另一个提示:在从 cookie 中获取样式之前,检查它是否存在(硬编码所有可用模板或 file_exists())。如果没有,请使用默认值(红色)。

于 2009-08-24T07:43:00.507 回答
0

我解决了这个问题......解决方案正在修复三到四个错误:

首先,html中的cookie检查器应该询问coockie是否为空,如果是SET则不。所以:

<?php  // Checks for, and assigns cookie to local variable:
if(!empty($_COOKIE['style'])) $style = $_COOKIE['style'];
// If no cookie is present then set style as "red" (default):
else $style = 'red';
?>

这是首先要解决的问题。然后这一行是固定的,因为 JQuery 插件要求在标头中更改一个 ID。我没有意识到我在链接样式表声明中没有 ID

<link id="stylesheet" rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/wh1/css/template_<?php echo $style ?>.css" media="screen" />

在这一行中,我还发现

<?php echo $this->baseurl ?>

应该用基地址替换,但没有域,所以它应该是

/gloom/templates/wh1/css/template_<?php echo $style ?>.css

另外,在 JQuery 插件的初始化中,我犯了一个致命的错误。我没有更改插件将要捕获的标签容器的 ID。这部分是因为样式表标题上的 ID 和标签容器上的 ID 之间的混淆。所以我使用“#tselect”作为 DIV 的 ID,它解决了 Jquery 工作。此外,必须在插件的每个语句中添加 $j。

$j(document).ready(function(){
    $j.preloadCssImages({statusTextEl: '#textStatus', statusBarEl: '#status'});
        $j("img#hhead").toggle(
        function () { 
            $j("#headtop").slideDown();
        },
        function () {
            $j("#headtop").slideUp();
        });
    $j("#tselect a").styleSwitcher(); // here is the "ID TAG".plugin() call.
});

现在,在 styleswitcher.php 中,我使用了有人告诉我关于 cookie 的提示:

<?php $style = $_GET['style'];
setcookie("style", $style, time()+604800,"/"); // 604800 = amount of seconds in one week
if(isset($_GET['js'])) echo $style;
else header("Location: ".$_SERVER['HTTP_REFERER']);
?>

你注意到了吗?是一个简单的 / 斜线。

现在一切正常。stylechanger 更改样式并且 JQuery 效果起作用。你可以在这里看到它:链接文本

如果有人想要文件和解释,请给我发消息。

于 2009-08-27T03:46:18.607 回答