PHP: Echo function output in function-line, not line of function call - php

I have a problem with my self-coded template system. The content is inserted by Include(). Now I need to add a meta redirect to one page. I know, meta redirect is not the safest way, but I need it because of it's delay possibility.
Now i'm looking for a way to influence the wrapping page (template) by the wrapped page (content).
So I thought a function can do this job.
<?php function test($testvar){
echo $testvar;}
?>
<hr />
<?php
test("testtext");
?>
Of course the text echos in the line of the function call, not in the function line. Is there a way to make the function echo in the line of the function itself? In this case above the horizontal rule, not below?
Of course every other best pratice for this "template problem" ist welcome!
THX
EDIT:
<html>
<title>testpage</title>
<? a_function_that_echos_a_metatag_for_redericetion($param){
echo $param;
}
?>
<head>
<body>
<? include("test.php");
INCLUDED script---->
a_function_that_echos_a_metatag_for_redericetion("\"<meta http-equiv=\"refresh\"...")

Procedural programming cannot have the logic you request.
If you want to add the meta redirect element before the page content, you HAVE to know that you are GOING to make this redirect before the <head> is closed.

Is there a way to make the function echo in the line of the function
itself?
No. Functions are declarations and aren't executed until they are called.
If you wish to modify the request headers later down the page, then you'll need to employ output buffering. However, this approach will likely be overkill if only one page needs the special header consideration. It may be better to just detect the page via some parameter (such as REQUEST_URI) and insert the header conditionally from that.

Related

Clear parts of a page before the PHP code

Hypothetical Question
I'm working on a thing, that is difficult to explain, but take a page PHP file like this
<html>
...content...
</html>
<?php
code();
?>
Is there a way for the code function to cause the contents of the html not to render. This is the goal. Keep in mind, I specifically don't want to place code at the head, like you normally would. Ultimately, I am trying to discount the procedural loading, I suppose.
Is there a way for the code function to cause the contests of the html not to render
<html>
...content...
</html>
Nope, you have lost all those rights after this point. So there is no Way following PHP code can stop it now. Unless you use auto_prepend_file or any webserver related hacks to process any other PHP code first which uses output buffering which stops any output being sent to the browser in the first place. But if in any case its ever sent you can't stop it after that.
<?php
code();
?>

Drupal 6 Rules Execute custom php output not in html body

I'm using drupal-6.22, rules-6.x-1.4, and some modules.
With the help of rules module, I've set a triggered rule, when the condition happens, it execute custom php code.
The custom PHP print some html like this:
<?php echo '<div><span>whatever</span></div>'; ?>
BUT, the html just appears before the !DOCTYPE html tag, is there any method to place the code wherever I want?
(e.g. <div id="header">HERE!</div>)
I think in this case JS is in need, so I use drupal_add_js($output,'inline'), and called an outside function to place the html, still nothing happens.
I think there must be some method to let PHP insert the output into the current DOM tree, is there any known function or variables for this?
Please help, thanks!!
drupal_set_message() seems to be the right thing for you.

PHP placement problem

I am calling this function halfway down the page:
<div id="leftArea">
<?php
if (isset($id)){
single($id);
}
?>
</div>
The problem is i want use some of the output in the meta tags in the <head>, whats the best way to approach this?
EDIT:
The single function, takes the $id and echo's the data straight to the page, exactly where it is. In the DIV leftArea. But i want to take one of the rows from the DB and insert it into the META tags at the top
Copy the code into the <head> section.
Redesign your System
The best method is to create a class that manages your html page for you, example:
$Page = new HTMLPage("My Page",HTMLPage::Strict);
$Page->addScript("....");
$Page->addScript("....");
$Page->addScript("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->SetBody($MyTemplate);
$Page->send();
this way though out your functions you can do
function myfunc()
{
global $Page;
$Page->addScript("....");
}
the main point here is you should build your document up before sending it to the browser, this way you still have control over the content no matter where your code is executing from.
on the final send method you build your content up, and then push the content via echo, and then exit directly. (all processing should be done prior to output to manage errors)

How could you create a "content_for" equivalent in PHP?

I've been working on a small page in PHP, one that doesn't need the power of a full-fledged framework behind it. One thing that I'm really missing from previous work in Ruby-on-Rails is the ability to effectively pass content up the page using "content_for".
What I was wondering is, how could you create a page lifecycle that would accomplish this same effect in PHP?
So, here's a simple example:
Let's say you have a template that defines an index page, just a recurring header and menu you want to use on all your pages. So your index.php file looks basically like this:
...header stuff...
<body>
<?php include $file.'.php'; ?>
</body>
...footer stuff...
EDIT: Thanks for the tips on URL security, but let's just assume I'm getting the user request safely :)
Now, lets say in the header you want to put this:
<head>
<title><?php echo $page_title; ?></title>
</head>
It would be nice to be able to specify the title in the included file, so at the url http://example.com/index.php?p=test you're loading test.php, and that file looks like this:
<?php $page_title = 'Test Page'; ?>
... rest of content ...
Now, obviously this doesn't work, because the including page (index.php) is loaded before the variable is set.
In Rails this is where you could pass stuff 'up the page' using the content_for function.
My question is this: What would be the simplest, leanest way that you all can think of to effect this kind of 'content_for' functionality in PHP?
Ideally I'd like suggestions that don't involve strapping on some big framework, but some relatively light boilerplate code that could be used in a lot of different applications.
Never do include $_GET['p']. This opens a huge security hole in your site, as include accepts filenames and URLs, so anybody would be able to read any file on your site and also execute any code on your server. You may want to check and sanitize the value first.
If you need something simple, you may put header and footer in separate files, execute your test.php which would set the variables, capture its output using output buffering, then include the header, output the middle part and include the footer. Example:
<?php ob_start(); ?>
<body>
<?php include $filename.'.php'; ?>
</body>
<?php $content = ob_get_clean();
include 'header.php';
echo $content;
include 'footer.php';
?>
If I understand you correctly (I have not used RoR extensively), you could put your data in a variable or a function. If your content was in a variable, your "test.php" could simply hold all your variables and you could load it at the very beginning of your index file (likewise for a function depending on how complicated your needs are; if you're doing a lot of extra work, you may need to use a function as a variable won't work).
For example, your test.php would look something like this:
<?php
$page_title = "Test Page";
$page_content = "Some sort of content";
// Or
function page_content()
{
// Run some functions and print content at the end
}
?>
Then, in your index.php
<?php include $_GET['p'].'.php'; ?>
...header stuff...
<title><?php print $page_title; ?></title>
<body>
<?php print $page_content; ?>
<!-- OR if function -->
<?php page_content(); ?>
</body>
...footer stuff...
This way everything should load properly. You could also split things up, but that would complicate your structure (especially if there is no need for an elaborate framework, this would be unnecessary).
Good luck!
Dennis M.
Are you worried about XSS? Or are you going to filter/whitelist the "filenames" from the query string?
My answer would be to use mod_rewrite -- if you're using PHP, you're likely using Apache!
You could filter out files with a RewriteCond and your RewriteRule could be:
RewriteRule /index.php?p=(.*)$ $1 [L,QSA]
This may be a different approach than the PHP functionality you were looking for, but it comes to mind...

What's the best way to automatically print an HTML header and bottom in php?

Currently I'm using:
PrintHeader();
/* Page code... */
PrintBottom();
Is there a better way?
Any output shouldn't be done automatically. As not every script call may return a page of text. So, you have to call page renderer manually.
So, you can make it with single call CallTemplateRender();
Not a much difference though
Its best to separate your business logic and display code, I typically use a master php page with a variable that includes an inner php page. Something like this:
<html>
<head></head>
<body>
include($template_file);
</body>
</html
Then my logic page works like this:
//do processing
$set_variables;
$template_file = "inner_file.php";
include("master_template.php");
Of course its a little more involved than that but all of my logic and display is separate so it's easier to manage.

Categories