Executing only the code of a page? - php

Hi guys consider I have a .php page that I need to include somewhere else.
page.php:
<?php
dosomecmd();
doothercmd();
//etc
?><html>pagehtml</html>
Is there a way to include this page but not printing the html inside? (I just need to execute those command)
I could do with ob
ob_start();
include('page.php');
ob_discard();
But i would prefer a faster way.
Thanks
Edit: i know i can sepearate html and php (and i already do) but that page.php is non else than a "static" cache I make, but sometimes I need to execute some command inside that cache instead to printing automatically it out
Edit2: of course i don't need everyime to not output the html (otherwise I could just delete all html) I need to return; only based on the results of my cmds up there
Thanks all i find a solution (add return;)

Would it be easier to have a common page with only the php that you need to share, and include it in both page.php and your other page?

you can try to seperate with
$_SERVER['REQUEST_URI']
also I wouldn't recommend that

Try to separate the PHP logic at the start of your current script into another file, then include that into both of your scripts.

Related

Include PHP file with condition opened but closed in another included PHP file

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.
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]
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.
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"

Proper way to require file that requires db connection

I don't use php very often and was wondering if someone could answer this question for me.
I have a folder structure like so:
-pages/rightCol.php
-pages/privacyPolicy.php
index.php
In my index file I have a connection to the database like this:
ob_start();
require($_SERVER['DOCUMENT_ROOT'] . "/inc/db.inc.php");
That works fine.
I wanted to separate out some repeated code between pages so I created the rightCol.php file. It needs the connection to the database. So right now I create a query result at the top of the index file and use the statement:
This works.
I also wanted to include it in the privacyPolicy.php page. This does not work because I do not want to put the query code at the top of every page that requires the rightCol.php file.
I would like to put the db stuff inside the rightCol.php. When I try this, then my privacyPolicy.php file works but then my index breaks. Probably because I require the db file twice, once at the top of the index and once in the rightCol.php file.
How can I set this up properly where I do not need to repeat code.
Thanks
EDIT
I changed my call to use require_once.
The privacyPolicy.php page works fine but when I view my index.php it has errors.
Error: No DB selected.
Include the db/inc.php only at the start of your index.php and open the connection. That way, it will stay open throughout the whole script. Then just close it at the very end of your site;
If you are having problem knowing where to include and not ( and still for some unknown reason want to include it more then once ), then get used to require_once method. This way the file will be included only once and the 2nd attempt will be ignored.
Well, the quick way to solve the problem is to use require_once. But i highly recommend that you use a micro-framework like Slim.

If file.html changed to file.php how to keep the jQuery working?

I had to change a file suffix from .html to .php so a PHP include works, but after doing that the jQuery does not work any more. Is that normal? Is there any way to keep jQuery working?
No, it is not normal. I don't see your code, but I have 2 assumption:
How do you call your php file? Do you call it same way as html? If you call html from local path (c:/dir1/dir2/file.htlm) and php from server (http://localhost/file.php) then possible reason is path to jQuery
What is php include? Try to remove PHP code and try again. (with PHP extension)
ps. Do you get any jQuery-related errors in your browser?
You have to handle it like HTML. It should stand outside the PHP Tags or you have to use echo or something similar.
Thanks Andray. It is working now. I must have been doing something wrong, or perhaps was not refreshing the code properly into my testing site. Yes, the php behaves very much as the original .html including the java scripts in it.

PHP function getHeader()

Hey guys, Im building a fairly large website here, I am using quite a bit of php along with it, but what I was wondering, I have a header that does't change throughout the website, and I was wondering if I could create a function in some of my php code where all I would have to do is call like a function getHeader() and it will return the header. Now this header has some php in it also like a search bar and a username container... I was just wondering if this was possible on a fairly simple scale so I don't have to place the header code in each php file. which is fine but if I happen to make an update I have to update file which could take some time...
Thanks in advance!
Just create a header file (e.g. header.php) and include it.
http://php.net/manual/en/function.include.php
You can either have the code directly in the header.php not in any functions, and it will run by default, or put it in a function and call it manually.
A simple include() at the top of every file is what you'll need http://php.net/manual/en/function.include.php
You could use a function. That's what WordPress does. You could even put PHP inside them. You'd just have to keep in mind that the PHP inside your function is... well... inside a function. So you'd have to explicitly access global variables, etc.
Another option would be to put the header in its own, single file and just include that file, instead of calling a function. Whatever floats your boat.

Storing the HTML output from a local PHP file into a string using file_get_contents

There is a header.php file and it contains some php codes that return HTML.
I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.
In a page, I used:
$headerHTML=file_get_contents('header.php');
Then I got the PHP code output rather than the processed HTML output.
I know adding http:// would help.
But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?
Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.
I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.
Don't use eval() - it's evil!
Use the relative local path an automatically map it to a absolute URL.
If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?
You can check http://in2.php.net/manual/en/function.eval.php#56641, hope it helps.

Categories