1

I would like to include at the beginning of my script a PHP file that open a IF condition. Then i write my script, and to finish I include another PHP file that close the conditon.

This bring me to a "Parse error: syntax error, unexpected end of file in ..." error.

This will be better to understand with this simple example :

header.php

if(aConditionalTest()) {

footer.php

} // endIf

mypage.php

include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';

FYI: I would like to do this for example :

  • to check everywhere that a user is authorized before displaying the content
  • implement a webpage caching system (see http://www.phpfastcache.com/ in "Example" section, "Caching Whole Webpage")

THANKS!


edit : Explain more precisely WHY I want to do this for using phpfastcache :

http://www.phpfastcache.com/ says :

Caching Whole Webpage PHP Cache whole web page : You can use phpFastCache to cache the whole webpage easy too. This is simple example, but in real code, you should split it to 2 files: cache_start.php and cache_end.php. The cache_start.php will store the beginning code until ob_start(); and the cache_end.php will start from GET HTML WEBPAGE. Then, your index.php will include cache_start.php on beginning and cache_end.php at the end of file.

That's just what I try to do! According to their piece of code below, this brings to the situation where the condition is opened in "cache_start.php" and then closed in "cache_end.php"

cache_start.php

 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();

cache_end.php

 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();

mypage.php

include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';

myotherpage.php

include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';

So the reason WHY I want to put the phpfastcache code in 2 separate files is that I have many different PHP pages to cache, so I would like to avoir repeating all this code on each page...

Hope this edit will help you better understand why I would do that, even if I understood, as I feared, that is is not possible.

TooLiPHoNe.NeT
  • 479
  • 1
  • 5
  • 22

3 Answers3

2

Give it a try:

how can I achieve this ?

Do it the evil way and eval all instead of including :) Like

eval(file_get_contents('header.php').'<?php echo "my awesome content";?>'.file_get_contents('footer.php'));

That can be a solution, if you want to join the dark side :)

SideNote: In this solution, you have to keep an eye on global variables!!

But please, thing about the fact, that you want to spread conditions over seperate files, what in my opinion is very very very bad practise.

Did i really answer this 8]

JustOnUnderMillions
  • 3,741
  • 9
  • 12
1

I try it in other way.

Only rule: works only in global space (where else :-))

So you want to open an if() in cache_start.php and close it cache_end.php. (for ob_cache reasons)

But if the condition isn't changed why not doing the condition twice!

In each file test for if(condition)!

Or set up an variabale like $cach_op_started=true and test for it in the second if() in cache_end.php

Thing boths should work for you.

Its a little funny that i didnt see that solution at the first time :)

Last Note: You can also use auto prepend and append files in PHP if you want to. That can be configurated in php.ini. The files will automaticly loaded before and after an script, always. http://www.webdevsecrets.com/using-phps-auto_prepend_file-and-auto_append_file/

Have a nice time.

JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • @DataHerder Look, its alive :-) – JustOnUnderMillions Oct 14 '16 at 14:39
  • @TooLiPHoNe.NeT The second code block makes it clear, the first shortend version, was really not clear enuff. After i know is all about `ob_cache` it is easy to answer. – JustOnUnderMillions Oct 14 '16 at 14:42
  • thanks that is a very simple solution and that works great! I've put a global boolean in the "start" included PHP file and used it for conditions in "end" PHP file and also in my "content" PHP file I'd like to cache... Don't know why I didn't think about it by myself... :) – TooLiPHoNe.NeT Oct 18 '16 at 07:44
0

So I understand that the problem is that when you don't want a certain user to see the contents of a page, the rest of the page isn't loaded and that is whats causing the error?

If so, why don't you just always include the files and in the specific page you set the conditions for who can view what?

I use ob_start() in my header, and ob_end_flush in my footer which works great. and then check with my SESSION variables on the specific page's content if the logged in user has the right to see the content, else display a message like:"You are not authorized to see this content"

Jorn
  • 457
  • 7
  • 11