SiteCake and my PHP are clashing - php

I am trying to install sitecake, I followed all the instructions by adding <?php include "sitecake/server/sitecake_entry.php"; ?> on the top of the page and classes to the divs that I want to be edited.
The problem is, when I add <?php include "sitecake/server/sitecake_entry.php"; ?> my PHP stops working, why is that? what I am doing wrong?
For example, here is one of my PHP scripts that is added to the pages using PHP include:
<!--add class .active to current page-->
<?php
$directoryURL = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURL, PHP_URL_PATH);
$components = explode('/', $path);
$currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));
if ($currentPage == "") {
$currentPage = "index";
}
function href($url) {
global $currentPage;
$path = explode('/', $url);
$page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
echo 'href="' . $url . '" ';
if ($page == $currentPage) {
echo 'class="active"';
}
}
?>
Here is the error I get when I use error_reporting(E_ALL):
Fatal error: Uncaught exception 'Zend_Session_Exception'
with message 'Session must be started before any output has been sent to the browser; output started in /home/approach/public_html/index.php/2'
in /home/approach/public_html/sitecake/server/library/Zend/Session.php:454
Stack trace:
#0 /home/approach/public_html/sitecake/server/library/Zend/Session/Namespace.php(143): Zend_Session::start(true)
#1 /home/approach/public_html/sitecake/server/application/services/impl/onesite/Bootstrap.php(21): Zend_Session_Namespace->__construct('Default', false)
#2 /home/approach/public_html/sitecake/server/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap->_initSession()
#3 /home/approach/public_html/sitecake/server/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('session')
#4 /home/approach/public_html/sitecake/server/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract->_bootstra in /home/approach/public_html/sitecake/server/library/Zend/Session.php on line 454

Your error message is this:
Session must be started before any output has been sent to the browser; output started in /home/approach/public_html/index.php/2
And the error occurred at:
/home/approach/public_html/sitecake/server/library/Zend/Session.php:454
The full explanation of the meaning of this message is already explained in detail at another answer, I highly recommend that you read this answer to get a better example of what's going on.
For your specific case, it would seem that you're including your sitecake_entry.php script at the top of the page, or, at least somewhere before you initialize Zend. What you want to do, is this (from the SiteCake examples page):
<?php include "sitecake/server/sitecake_entry.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I don't know the details of Zend, or your application in particular, the thing you probably want to do is find your HTML template, you could search your projects's directory for <!DOCTYPE, and add the line above that, as per the example, instead of adding it to your index.php.

Related

Why are the html-tags inside <noscript> shown as text

I have a noscript part:
<noscript>
<h1><?php echo $php_title; ?></h1>
<div><?php echo $php_abstract; ?></div>
</noscript>
When I try this in a .html file (with the php tags removed, of course) it works as expected, but when in a .php file I get this visible output in the browser (i.e. the is not treated as an html tag):
<h1>Stephen Porges "The Polyvagal Theory"</h1>
I do not set any special headers with PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'HTTP/Request2.php';
$url = "https://api.zotero.org/groups/56508/items/3B6TR25A?format=atom&content=json";
$r = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$r->setConfig(array(
'ssl_verify_peer' => FALSE,
'ssl_verify_host' => FALSE
));
try {
$response = $r->send();
if ($response->getStatus() == 200) {
$body = $response->getBody();
$xml = new SimpleXMLElement($body);
$php_content = $xml->content;
$php_json = json_decode($php_content);
$php_title = $php_json->title;
$php_abstract = $php_json->abstractNote;
}
} catch (HttpException $ex) {
echo $ex;
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
The document is returned as text/html according to the browser.
I use Chrome and there is maybe a bug here, but it seems strange that it works ok in .html but not .php if that is the reason.
Any idea of what is going on here?
This is an issue with chrome where it is not rendered the first time the browser loads the page with JavaScript disabled. If you refresh the page after it shows plain text, it should be rendered properly.
This issue has already been reported:
https://code.google.com/p/chromium/issues/detail?id=235158
One person from that website suggested a workaround of using something like:
<div id="noscript">What was in the noscript-tag ..... </div>
<script type="text/javascript">
document.getElementById('noscript').style.display="none";
// rest of script
</script>
Therefore, it would only hide the elements if JavaScript is enabled.
I wouldn't say that it is that big of a deal. For example, Stack Overflow uses <noscript> tags regardless and the same occurs.

PHP include() wiping rest of HTML document?

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.

Undefined index: page in

Ok I have the above error on my page, I am using PHP 5.3 for the first time, and have used this code before but never had this notice, so I was hoping for some guidance. I have looked at some questions but when I try to apply the solution to my bit of code it doesn't seem to work.
This code is a PHP pagination script from paper mashup and so if I can find the answer it may help anybody else who is also using this code and experiences the same problem.
the piece of code which is generating the notice is this -
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
From what I have read it was suggested to add isset and so changed the code to look like it does below, however i still get the same error.
$page = mysql_real_escape_string($_GET['page']);
if(!isset($page)){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
Any advice would be appreciated.
Thanks
Stan
The 'undefined index' error is basically telling you that you tried to get something from an array using an index for which there is no matching element.
In this case, the problem is in the line above your isset() call. The index 'page' is not defined in your $_GET array. So you need to first check if $_GET['page'] is set:
if (isset($_GET['page'])) {
$page = mysql_real_escape_string($_GET['page']);
// do more stuff
}
i got some issue.. this is the best and shortest solution i've got..
$page = (isset($_GET["page"]) ? $_GET["page"]:$config["per_page"] ='');
or
$page = (isset($_GET['page'])) ? $_GET['page'] : 0;
The error is on your first line
do
$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0;
In your code you check wether the page is not set, and then use the page, resulting in the same notice. Simply remote the exclamation mark. Try this:
$page = mysql_real_escape_string($_GET['page']);
if(isset($page)){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
Although I can't see it in your code, I would be willing to bet that you have an empty variable $_GET['page'] in your code. When you do a pagination, you need to keep appending the variable to the anchor tags if you are using the <a href="somePage.php?page=4 style of link.
Check your links to make sure that you keep passing it back all the time.
The error message undefined index appears when you try to access an array element that doesn't exist - and in this case, it is almost certainly the $_GET array.
Hope this will help
<?php
# default page
$default = 'home.php';
# set document root path
$base = $_SERVER['DOCUMENT_ROOT'].'/redirect/';
# list of all site pages + the id they will be called by
$pages = array('home' => 'home.php','about' => 'about.php','contact' => 'contact.php');
if (isset($_GET['page'])) {
$page = mysql_real_escape_string($_GET['page']);
if(array_key_exists($_GET['page'], $pages))
{
foreach($pages as $pageid => $pagename) {
if($_GET['page'] == $pageid && file_exists($base.$pagename))
{
/* if somebody's making a request for ?page=xxx and
the page exists in the $pages array, we display it
checking first it also exists as a page on the server */
include $base.$pagename;
}
} // end foreach
}
else {
/* if the page isn't listed in $pages, or there's no ?page=xxx request
we show the default page, again we'll also just make sure it exists as a file
on the server */
if(file_exists($base.$default)) include $base.$default;
}
} else {
header ("Location: index.php?page=home");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body>
<div style="height:50px;border:1px solid #000;margin-bottom:10px">
</div>
<div style="float:left;width:15%;border:1px solid #000">
<ul style="margin-left:10px;list-style-type:none">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
<div style="float:right;width:80%;border:1px solid #000">
<div style="padding:4px">
<!-- content here -->
</div>
</div>
</body>
</html>
You should do it like this
if(isset($_GET['page']){
$page = mysql_real_escape_string($_GET['page']);
if(!isset($page)){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
}
Undefined index: page refers to some array specific error. In your code the array with index value page is $_GET so it should be the first line of your code that needs some protection.

Colour Extract Script - ForEach Error

So I downloaded and edited a script off the internet to pull an image and find out the hex values it contains and their percentages:
The script is here:
<?php
$delta = 5;
$reduce_brightness = true;
$reduce_gradients = true;
$num_results = 5;
include_once("colors.inc.php");
$ex=new GetMostCommonColors();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Colour Verification</title>
</head>
<body>
<div id="wrap">
<img src="http://www.image.come/image.png" alt="test image" />
<?php
$colors=$ex->Get_Color("http://www.image.come/image.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
$success = true;
foreach ( $colors as $hex => $count ) {
if ($hex !== 'e6af23') {$success = false; }
if ($hex == 'e6af23' && $count > 0.05) {$success = true; break;}
}
if ($success == true) { echo "This is the correct colour. Success!"; } else { echo "This is NOT the correct colour. Failure!"; }
?>
</div>
</body>
</html>
Here is a pastebin link to the file colors.inc.php
http://pastebin.com/phUe5Pad
Now the script works absolutely fine if I use an image that is on the server, eg use /image.png in the Get_Color function. However, if I try and use an image from another website including a http://www.site.com/image.png then the script no longer works and this error appears:
Warning: Invalid argument supplied for foreach() in ... on line 22
Is anyone able to see a way that I would be able to hotlink to images because this was the whole point of using the script!
You must download a file to the server and pass its full filename to the method Get_Color($img) as $img parameter.
So, you need to investigate another SO question: Download File to server from URL
The error indicates that the value returned by Get_Color is not a valid object that can be iterated on, probably not a collection. You need to know how the Get_Color works internally and what is returned when it doesn't get what it wants.
In the mean-time, you can download [with PHP] the image from the external url into your site, and into the required folder and read the image from there.
$image = "http://www.image.come/image.png";
download($image, 'folderName'); //your custom function
dnld_image_name = getNameOfImage();
$colors=$ex->Get_Color("/foldername/dnld_image_name.png");
By the way, did you confirm that the image url was correct?

Url and title with php snippet

I read several similar posts but I don't see my fault.
index.php looks like:
<head>
<title>Demo Title</title>
</head>
<body>
<?php
require_once "footer.php";
?>
</body>
footer.php looks like:
<?php
/*
* _$ Rev. : 08 Sep 2010 14:52:26 $_
* footer.php
*/
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo $url;
$file = # fopen($_SERVER[$url],"r") or die ("Can't open HTTP_REFERER.");
$text = fread($file,16384);
if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
$title = $found[1];
} else {
$title = " -- no title found -- ";
}
?>
A request for the URL http://127.0.0.1/test/index.php results in:
http://127.0.0.1/test/index.phpCan't open HTTP_REFERER.
or for http://127.0.0.1/test/
http://127.0.0.1/test/Can't open HTTP_REFERER.
Any hints appreciated.
$_SERVER is an array which contains a bunch of fields relating to the server config. It does not contain an element named "http://".$host.$param, so trying to open that as a filename will result in the fopen call failing, and thus going to the die() statement.
More likely what you wanted to do was just open the file called "http://".$host.$param. If that's what you want, then just drop the $_SERVER[] bit and it should work better.
Note that because it's a URL, you will need your PHP config to allow opening of remote files using fopen(). PHP isn't always configured this way by default as it can be a security risk. Your dev machine may also be configured differently to the system you will eventually deploy to. If you find you can't open a remote URL using fopen(), there are alternatives such as using CURL, but they're not quite as straightforward as a simple fopen() call.
Also, if you're reading the whole file, you may want to consider file_get_contents() rather than fopen() and fread(), as it replaces the whole thing into a single function call.
try this:
$file = # fopen($url,"r") or die ("Can't open HTTP_REFERER.");
Try
<?php
$dom = new DOMDocument();
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo 'getting title for page: "' . $url . '"';
$dom->loadHTML($url);
$dom->getElementsByTagName('title');
if ($dom->length)
{
$title = $dom->item(0);
echo $title;
}
else
{
echo 'title tag not found';
}
?>
I can see your trying to track the referral's title
You need to use $_SERVER['HTTP_REFERER']; to get that
what you want to do is something like this
$referrer = (!empty($_SERVER['HTTP_REFERER']) && !substr($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_REFERER'] : false);
if($referrer)
{
try
{
if(false !== ($resource = fopen($referrer,"r")))
{
$contents = '';
while($contents .= fread($resource,128))
{}
if(preg_match('/<title>(.*?)<\/title>/is',$contents,$found))
{
echo 'Referrer: ' $found[1];
}
}
}catch(Exception $e){}
}

Categories