简体   繁体   English

如果是index.php,则显示“this”,如果没有,则显示“this”

[英]If index.php, show “this”, if not, show “this”

Desperately hoping someone can assist with this. 拼命希望有人可以帮忙解决这个问题。 I'm a novice with php. 我是php的新手。 I try and self teach myself through tutorials and I've searched high and low to no avail. 我尝试通过教程自我教学,我搜索高低无济于事。

Basically I'm looking to implement an "If index.php page, show foo , if not on index.php page, show bar " 基本上我想要实现一个“If index.php页面,显示foo ,如果没有在index.php页面上,显示bar

Any ideas? 有任何想法吗?

I hope I explain this well enough... 我希望我能够解释得这么好......

index.php includes a sidebar: index.php包含一个侧栏:

require_once('./cache/templates/sidebar.php');

Every subsequent page is built uses what's defined in this index.php file, meaning the sidebar.php is a must. 构建的每个后续页面都使用此index.php文件中定义的内容,这意味着sidebar.php是必须的。

I'm wanting to edit sidebar.php to contain an advert which displays solely on the index page. 我想编辑sidebar.php以包含仅在索引页面上显示的广告。

At the moment, when I edit sidebar.php, so for instance, to display the letter "B", it will display on the homepage, and every other page like so; 目前,当我编辑sidebar.php时,例如,显示字母“B”,它将显示在主页上,而其他每个页面都会显示;

Index Page: http://img.photobucket.com/albums/v684/nilsatis/1stack.jpg 索引页: http//img.photobucket.com/albums/v684/nilsatis/1stack.jpg

Every other page: http://img.photobucket.com/albums/v684/nilsatis/2stack.jpg 每隔一页: http//img.photobucket.com/albums/v684/nilsatis/2stack.jpg

How can I dictate one area of an included file to display on one page but exclude showing on others? 如何指示包含文件的一个区域显示在一个页面上,但不包括在其他页面上显示?

Any assistance would be very appreciated. 非常感谢任何帮助。

[Edit] This is the website in question: www.grandoldteam.com . [编辑]这是有问题的网站:www.grandoldteam.com。 You can see where I have the text "Test" - this was entered in sidebar.php. 你可以看到我在哪里有“测试”文本 - 这是在sidebar.php中输入的。 I'd like this text (future advert) to feature only on the index page, nowhere else. 我希望这篇文章(未来的广告)只在索引页面上展示,而不是其他地方。

[Edit 2] This is the point in which sidebar.php is called in the index.php file; [编辑2]这是在index.php文件中调用sidebar.php的点;

<p class="page_desc">'.$PAGE['subtitle'].'</p>
' );
            }

            if (isset($url[1])) require_once('./cache/html/'.$url[0].'/'.$url[1].'.php');
            else require_once('./cache/html/'.$url[0].'.php');
        }
    }

    require_once('./cache/templates/sidebar.php');



}


require_once('./cache/templates/footer.php');

And this is the but in which I can edit sidebar.php to display wanted text; 这就是我可以编辑sidebar.php来显示想要的文本;

<div class="clear"></div>
test
        </div>
<p>
    </div>

To do it the way you want, use the $_SERVER superglobal. 要按照您想要的方式执行,请使用$ _SERVER超全局。 The script's name is found in more than one place. 脚本的名称可以在多个位置找到。

if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) // index page...

Another option is to have index.php set some variable like $show_ad before including the side bar, and the side bar page can check that variable. 另一个选择是让index.php在包含侧栏之前设置一些变量,如$ show_ad,侧栏页面可以检查该变量。

I would not recommend to retrieve the name of the caller script, because several pages can have the same name in different folders, and also because you may want to change page names in the future. 我不建议检索调用者脚本的名称,因为几个页面在不同的文件夹中可以具有相同的名称,并且还因为您可能希望将来更改页面名称。

Use a global variable or a constant that says which page is the caller. 使用全局变量或常量来说明哪个页面是调用者。

index.php: index.php文件:

<?php
  $GLOBALS['caller_page'] = 'index';
  require_once('./cache/templates/sidebar.php');
  ...

sidebar.php: 的sidebar.php:

<?php
  ... 
  if ( isset($GLOBALS['caller_page']) && ($GLOBALS['caller_page']=='index') ) {
    ... // special action for the index page 
  }
  ...

Try this instead, 试试这个,

Create a session on the page where you only want to show "foo". 在页面上创建一个只想显示“foo”的会话。

Then do this 然后这样做

if ($_SESSION['valid']) {

//if the session is present, then show

}

else {

//if not,

}

This is not only a better way of going about it as what happens if your filenames get changed? 如果您的文件名发生变化,这不仅是一种更好的方式吗? This way it doesn't matter as it is checking against a session, not something that could change :) 这种方式无关紧要,因为它正在检查会话,而不是可能改变的事情:)

How to work it out: 如何解决:

At the top sidebar.php, add the line: 在顶部sidebar.php,添加以下行:

print_r($_SERVER);

This will show you the full contents of the $_SERVER variable. 这将显示$ _SERVER变量的完整内容。 From that, you should see a number of candidates that could be used, as well as other things that may be useful to you at some point. 从那时起,你应该看到一些可以使用的候选者,以及在某些时候可能对你有用的其他东西。

Once you've decided on what to use, you will need to check whether it includes the string index.php . 一旦确定了要使用的内容,就需要检查它是否包含字符串index.php A good way to do that would be to use: 一个好方法是使用:

if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) {

}

This idiomatic line checks to see whether the position of the string index.php in the script name isn't false, that is, it is a value. 这个惯用线检查脚本名称中字符串index.php的位置是否为false,即它是一个值。

Test the value 测试价值

$_SERVER[ 'REQUEST_URI' ]

against the literal 反对文字

'/index.php'. “的index.php”。

If it is identical. 如果它是相同的。 you index.php is executing. 你index.php正在执行。

all of those answers are great, but i provide a different practice. 所有这些答案都很棒,但我提供了不同的练习。 It's not better, but in my feeling it's more comfortable. 它并不是更好,但在我看来它更舒服。

when you do this: 当你这样做:

require_once('./cache/templates/sidebar.php');

on index.php, do this instead: 在index.php上,改为:

require_once('./cache/templates/sidebar.php?ad=1');

now, on sidebar.php, add this: 现在,在sidebar.php上,添加:

if(isset($_GET['ad'])&& $_GET['ad']=='1')
 // display ad

else
//don't display ad

EDIT: 编辑:

you want working code, fine... 你想要工作代码,好......

lets say your ad is actually the image of stackoverflow logo: 假设您的广告实际上是stackoverflow徽标的图片:

now on sidebar.php you'll have somthing like: 现在在sidebar.php你会有类似的东西:

<?php
....


if(isset($_GET['ad'])&& $_GET['ad']=='1')
  {
 ?>
<div>
<a href="http://stackoverflow.com">
<img src="http://blog.vicompany.nl/wp-content/uploads/2011/04/stackoverflow-logo-250.png" alt="ad"/>
</a>
</div>
<?php

} 

else
{
}

....
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM