I have a simple template rendering system that includes a header, template and footer PHP file. The header has a single PHP function call to render a navigation bar. The template has a function call that sets a header('location ...'); and the footer is just basic HTML.
When I view the page, I get a 'headers already sent' error, specifying the line in the header that makes the call to echo out the navigation bar. If I modify the code to remove the child elements in the navigation, I don't get the 'headers already sent' error. The child elements are nothing more than array items. If I replace them with dummy text, then the page redirects correctly.
I imagine it's something very simple, but it has me stumped!
header.php (the int passed in specifies the depth to scan)
<ul class="nav navbar-nav"><?php echo getNavigation(1); ?></ul>
template.php
// Run from start
default:
$_SESSION['updateStart'] = microtime(true);
$_SESSION['updateErrors'] = array();
$_SESSION['updateLog'] = array();
header('location: /update-products?action=getDataFeeds');
exit();
break;
getNavigation function:
function getNavigation($depth) {
global $path, $misc;
$items = $misc->getNavigation(1,(int)$depth,true);
$nav = "";
if($items) {
foreach($items as $item) {
$class = ($item['ob_alias']==("/".$path[0])?' class="active"':'');
$nav .= '<li'.$class.'>';
if(isset($item['children'])) {
$nav .= ''.$item['ob_label'].' <span class="caret"></span>';
$nav .= '<ul class="dropdown-menu" role="menu">';
foreach($item['children'] as $child) {
$class = ((isset($path[1]) && $child['ob_alias']==("/".$path[1]))?' class="active"':'');
$nav .= '<li'.$class.'>'.$child['ob_label'].'</li>';
}
$nav .= '</ul>';
} else {
$nav .= ''.$item['ob_label'].'';
}
$nav .= '</li>';
}
}
return $nav;
}
I've tried to walk through my code and all that I can seem to do to toggle the error Vs. having the page run as expected is to replace this line in my getNavigation() call:
$nav .= '<li'.$class.'>'.$child['ob_label'].'</li>';
with
$nav .= '<li>Some dummy text</li>';
Then it's fine. I can't see why including the two array item strings would break the 'headers'.
Any help, or pointers for things to test would be most welcome.
Seeing the order you are loading all of this may help but based on what you have, I would suspect you are rendering something (like the nav) before setting the headers in template.php. You may want to reference this answer as well as post more specific code that show in what order the template files are being loaded.
Functions that send/modify HTTP headers must be invoked before any output is made.
Otherwise the call fails.
Output can be:
Unintentional:
Whitespace before <?php or after ?>
UTF-8 Byte Order Mark
Previous error messages or notices
Intentional:
print, echo and other functions producing output (like var_dump)
Raw <html> areas before <?php code.
Related
I am writing a php plugin for wordpress. I'm trying to be mindful of clean coding and want to know the best practice for returning several lines of HTML code in an IF statement.
Obviously I know about echo but I thought I had seen a technique like this used, but it doesn't seem to work for me. The idea being that you create several $content variables and then return it outside of the IF statement.
function signup() {
if(!Skizzar_Registration::is_skizzar_site_active()) {
$content = '<div class="signup">';
$content .= '<h1>Sign up</h1>';
$content .= '</div>';
} else {
$content = '<div class="signed_up">you are already signed up</div>';
}
return $content;
}
Currently though this returns nothing when I call the function
As you have already discovered, the specific issue is that you are not ecxhoing the data.
As per your followon question (which is prefered, return or echo), in wordpress there seems to be a convention where both options are offered with the functions named accordingly:
//echos
function the_signup_form(){
echo get_the_signup_form();
}
//returns
function get_the_signup_form(){
if(!Skizzar_Registration::is_skizzar_site_active()) {
$content = '<div class="signup">';
$content .= '<h1>Sign up</h1>';
$content .= '</div>';
} else {
$content = '<div class="signed_up">you are already signed up</div>';
}
return $content;
}
I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});
So I have a simple html page that looks like this.
<html>
<head>
<?php include("scripts/header.php"); ?>
<title>Directory</title>
</head>
<body>
<?php include("scripts/navbar.php"); ?>
<div id="phd">
<span id="ph">DIRECTORY</span>
<div id="dir">
<?php include("scripts/autodir.php"); ?>
</div>
</div>
<!--Footer Below-->
<?php include("scripts/footer.php"); ?>
<!--End Footer-->
</body>
</html>
Now, the problem is, when I load the page, it's all sorts of messed up. Viewing the page source code reveals that everything after <div id="dir"> is COMPLETELY GONE. The file ends there. There is no included script, no </div>'s, footer, or even </body>, </html>. But it's not spitting out any errors whatsoever. Just erasing the document from the include onward without any reason myself or my buddies can figure out. None of us have ever experienced this kind of strange behavior.
The script being called in question is a script that will fetch picture files from the server (that I've uploaded, not users) and spit out links to the appropriate page in the archive automatically upon page load because having to edit the Directory page every time I upload a new image is a real hassle.
The code in question is below:
<?php
//Define how many pages in each chapter.
//And define all the chapters like this.
//const CHAPTER_1 = 13; etc.
const CHAPTER_1 = 2; //2 for test purposes only.
//+-------------------------------------------------------+//
//| DON'T EDIT BELOW THIS LINE!!! |//
//+-------------------------------------------------------+//
//Defining this function for later. Thanks to an anon on php.net for this!
//This will allow me to get the constants with the $prefix prefix. In this
//case all the chapters will be defined with "CHAPTER_x" so using the prefix
//'CHAPTER' in the function will return all the chapter constants ONLY.
function returnConstants ($prefix) {
foreach (get_defined_constants() as $key=>$value) {
if (substr($key,0,strlen($prefix))==$prefix) {
$dump[$key] = $value;
}
}
if(empty($dump)) {
return "Error: No Constants found with prefix '" . $prefix . "'";
}
else {
return $dump;
}
}
//---------------------------------------------------------//
$archiveDir = "public_html/archive";
$files = array_diff(scandir($archiveDir), array("..", "."));
//This SHOULD populate the array in order, for example:
//$files[0]='20131125.png', $files[1]='20131126.png', etc.
//---------------------------------------------------------//
$pages = array();
foreach ($files as $file) {
//This parses through the files and takes only .png files to put in $pages.
$parts = pathinfo($file);
if ($parts['extension'] == "png") {
$pages[] = $file;
}
unset($parts);
}
//Now that we have our pages, let's assign the links to them.
$totalPages = count($pages);
$pageNums = array();
foreach ($pages as $page) {
//This will be used to populate the page numbers for the links.
//e.g. "<a href='archive.php?p=$pageNum'></a>"
for($i=1; $i<=$totalPages; $i++) {
$pageNums[] = $i;
}
//This SHOULD set the $pageNum array to be something like:
//$pageNum[0] = 1, $pageNum[1] = 2, etc.
}
$linkText = array();
$archiveLinks = array();
foreach ($pageNums as $pageNum) {
//This is going to cycle through each page number and
//check how to display them.
if ($totalPages < 10) {
$linkText[] = $pageNum;
}
elseif ($totalPages < 100) {
$linkText[] = "0" . $pageNum;
}
else {
$linkText[] = "00" . $pageNum;
}
}
//So, now we have the page numbers and the link text.
//Let's plug everything into a link array.
for ($i=0; $i<$totalPages; $i++) {
$archiveLinks[] = "<a href='archive.php?p=" . $pageNums[$i] . "'>" . $linkText[$i] . " " . "</a>";
//Should output: <a href= 'archive.php?p=1'>01 </a>
//as an example, of course.
}
//And now for the fun part. Let's take the links and display them.
//Making sure to automatically assign the pages to their respective chapters!
//I've tested the below using given values (instead of fetching stuff)
//and it worked fine. So I doubt this is causing it, but I kept it just in case.
$rawChapters = returnConstants('CHAPTER');
$chapters = array_values($rawChapters);
$totalChapters = count($chapters);
$chapterTitles = array();
for ($i=1; $i<=$totalChapters; $i++) {
$chapterTitles[] = "<h4>Chapter " . $i . ":</h4><p>";
echo $chapterTitles[($i-1)];
for ($j=1; $j<=$chapters[($i-1)]; $j++) {
echo array_shift($archiveLinks[($j-1)]);
}
echo "</p>"; //added to test if this was causing the deletion
}
?>
What is causing the remainder of the document to vanish like that? EDIT: Two silly syntax errors were causing this, and have been fixed in the above code! However, the links aren't being displayed at all? Please note that I am pretty new to php and I do not expect my code to be the most efficient (I just want the darn thing to work!).
Addendum: if you deem to rewrite the code (instead of simply fixing error(s)) to be the preferred course of action, please do explain what the code is doing, as I do not like using code I do not understand. Thanks!
Without having access to any of the rest of the code or data-structures I can see 2 syntax errors...
Line 45:
foreach ($pages = $page) {
Should be:
foreach ($pages as $page) {
Line 88:
echo array_shift($archiveLinks[($j-1)];
Is missing a bracket:
echo array_shift($archiveLinks[($j-1)]);
Important...
In order to ensure that you can find these kinds of errors yourself, you need to ensure that the error reporting is switched on to a level that means these get shown to you, or learn where your logs are and how to read them.
See the documentation on php.net here:
http://php.net/manual/en/function.error-reporting.php
IMO all development servers should have the highest level of error reporting switched on by default so that you never miss an error, warning or notice. It just makes your job a whole lot easier.
Documentation on setting up at runtime can be found here:
http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
There is an error in scripts/autodir.php this file. Everything up to that point works fine, so this is where the problem starts.
Also you mostlikely have errors hidden as Chen Asraf mentioned, so turn on the errors:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Just put that at the top of the php file.
I'm using this to load php functions and send them to javascript in a plugin, like:
function me_nav_query_submit() {
$urlcall = nav_me_paises(); /* fetches a large html string */
$response = json_encode($urlcall); /* encode to display using jQuery */
//header( "Content-Type: application/json" );
echo $response;
exit;
}
I insert the html on the page, using
function(response) {
jQuery('#navcontainer').html(response);
}
and everything works fine, except that i get a "null" string at the very end of the result.
json_encode() documentation talks about null strings on non-utf-8 chars, but this doesn't seem to be the case. I've also tried using utf8_encode() with no success. I've read a bunch of other questions here on SO, but most of them either talk about one given value returned as null or bad UTF-8 encoding and in my case everthing just works, and then append "null" to the end.
note: Defining that header() call is recommended in the WP Codex, but i commented it because it was giving a "headers already sent" error.
Any ideas?
EDIT this is the function called:
function nav_me_paises() {
?>
<ul class="navcategorias">
<?php $tquery = $_POST['wasClicked']; ?>
<?php $navligas = get_terms($tquery,'hide_empty=0') ?>
<?php foreach ($navligas as $liga) : ?>
<?php $link = get_term_link($liga); ?>
<li class="liga"><a href="<?php echo $link; ?>" ><?php echo $liga->name; ?></a></li>
<?php endforeach; ?>
</ul>
<?php
}
nav_me_paises() is not returning anything. the html block is treated as output!
function nav_me_paises() {
$output = '<ul class="navcategorias">';
$tquery = $_POST['wasClicked'];
$navligas = get_terms($tquery,'hide_empty=0')
foreach ($navligas as $liga) {
$link = get_term_link($liga);
$output .= '<li class="liga"><a href="'.$link.'" >'.$liga->name.'</a></li>';
}
$output .='</ul>';
return $output;
}
nav_me_paises() doesn't return anything. Passing this "nothing" to json_encode() gives "null". Convert the function so that it returns the HTML instead of outputting it
function foo()
{
};
var_dump(json_encode(foo()));
string(4) "null"
Also, if it's just plain HTML, why json it? Just send it to JS, it will be a string stored in a variable, and you handle it normally.
I presume all you wanna do is put that HTML inside some div, because you'd not parse it into a DOM and process its elements... because if u'd do that u'd not use HTML for it.
I have a problem with 3rd-party-system integration in my drupal site.
Sorry for my english, i'm from russia, but i will try to explain my problem well.
Integration idea:
2 .php files
2 php-script lines (include
function's)
The problem is:
this scripts call to outside perl
(.pl) script. Perl script read the
parameters (parameters transfers by
url) and generate content.
I can't see this perl script, but i
know - hes working, but not in my
page :)
2 php files:
spectrum_view.php
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo $content = file_get_contents($url);
?>
spectrum_form.php
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params))
{
$url .= "?";
foreach ($params as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo iconv("windows-1251","utf-8",(file_get_contents($url)));
?>
and the 2 php-lines, wich i insert in my drupal pages
(the first i insert in page http://new.velo-travel.ru/view
and the second in the right block)
include("http://new.velo-travel.ru/themes/themex/spectrum_view.php?$QUERY_STRING");
include("http://new.velo-travel.ru/themes/themex/spectrum_form.php?act=/view$QUERY_STRING");
So, i solved this problem, but not in drupal - only on my Localohost, i just create a 2 page:
form.php:
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params)){
$url .= "?";
foreach ($params as $keys=>$value) $url .= "&".$keys."=".urlencode($value);
}
$content = file_get_contents($url);
print $content;
**require_once 'view.php';**
?>
view.php:
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
}
$content = file_get_contents($url);
print $content;
?>
=(
I'm not entirely sure, as to what you are trying to do. But it seems like you want to generate this content from the perl script. If this is a special page with it's own template, you should move all this code into template.php. This file is made to hold some logic you want to create the content for your page.
Personally I would prefer to make a module to handle all this, but it's probably easier to do this in the theme, with what you got now. It seems like you are making a form, and some content based on the form. This could be done in a module. You could create a Drupal form, and then handle the validation with drupal, and jst submit the data to perl. But if you would want to get it from perl, going with the theme is probably best. So how do you do it?
Implement a preprocess function for the tpl.php file you use.
Create all the logic here, you could copy the php files you use over or just include them. Import, assign the result to a variable the will be accessible in the template file.
Print the variable in your template.
In code this would look something like this:
//template.php file
function mytheme_preprocess_somename(&$vars) {
include('php');
// Do some logic.
$vars['form'] = $result_a;
$vars['my_content'] = $result_b;
}
// your .tpl.php
// Some markup here
<div><?php print $my_content; ?></div>
<div><?php print $form; ?></div>
Now, I'm not sure exactly what you are after, but something like this should help you along. Note it's important what you call your variables inside the template file, as you can overwrite some Drupal variables like $content, which can cause some bugs.
You probably are running into a security issue. Please note allow_url_fopen and allow_url_include - these settings must have accordant settings in your php.ini. Otherwise you can't e.g. include a remote file for security reasons.