...

View Full Version : How can I put PHP code into an HTML file being required?



cineweekly.com
01-20-2012, 02:51 AM
Sorry, I'm not sure of the terminology I'm very new to PHP and still learning.
First of all, here is my html code:


<head>
<?php require("head.html"); ?>
</head>
<body>
<div class="link"><a href=" '.$link.' ">Random</a></div>
</body>


How can I insert the following code into the head.html file? Currently, the file only contains <?php echo ' Some html '; ?>

// Predefinitions
$links = file('/articles.txt');
$currentPage = $_SERVER['PHP_SELF'];

// make sure links are pure and no whitespaces are left
foreach ( $links as &$link )
{
$link = trim($link);
}

// prepare for history
if ( ! array_key_exists('visited_links', $_SESSION) )
{
$_SESSION['visited_links'] = array();
}
// add current site to visited links if it wasnt visited earlier
if ( ! in_array($currentPage, $_SESSION['visited_links']) )
{
$_SESSION['visited_links'][] = $currentPage;
}

// get all those links which were not visited yet kinda $links = $allLinks - $visitedLinks
$potentialLinks = array_diff($links, $_SESSION['visited_links']);

$randomLinkId = mt_rand(0, count($potentialLinks)-1);
$randomLinkIds = array_keys($potentialLinks);
$randomLink = $potentialLinks[$randomLinkIds[$randomLinkId]];

djh101
01-20-2012, 02:58 AM
Just insert it? And, of course, change head.html to head.php.

cineweekly.com
01-20-2012, 04:35 AM
Just insert it? And, of course, change head.html to head.php.

My html parses as php server-side.
Where do I insert the php? I've tried this format:



<?php
php code from original post

echo '
html
';
?>



But I'm getting this error in my error log:

PHP Warning: array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists</a>]: The second argument should be either an array or an object

This is the line in question:
if ( ! array_key_exists('visited_links', $_SESSION) )

The code works when the link is echoed from within the php (from earlier) so the problem has to be the location I'm inserting the php.

cineweekly.com
01-23-2012, 06:12 AM
Any idea on why I'm getting this error? It's only an error when inserting it through php. In a plain html doc there are no errors.

BluePanther
01-23-2012, 06:24 AM
If you're mixing logic and markup (html), which I don't recommend, then you can use ?> and <?php to stop parsing as PHP and treat as plain text. So,



<?php
// Some php code
?>
html and plain text
<?php
// Some more PHP
?>


The most likely explanation for your error is unescaped quotation marks - echo "<div style="stuff">" as an example.

cineweekly.com
01-23-2012, 11:05 AM
I fit the php in properly as you said and tried every way to escape things but the problem persists. Same error message.

Fou-Lu
01-23-2012, 02:21 PM
Session superglobal is constructed during session_start. You need to have this call within any script that uses it.

cineweekly.com
01-23-2012, 10:45 PM
Session superglobal is constructed during session_start. You need to have this call within any script that uses it.

The code works completely on it's own, it only gives the error when I insert it in this manner. On my individual pages I have a php require calling for the code that makes up my sidebar. In that sidebar file I have a php echo then I close it and insert the code from above, close it, and then have another php echo with the rest of my code.

As I said, when I have the code on the individual page rather than inside the required file, it works fine.

Fou-Lu
01-24-2012, 12:06 AM
Do you mean the other way around? I can see this working fine when included into another script that does issue a session_start. Stand alone it will not work as $_SESSION is not constructed without the call to session_start. Since the cost to use overhead is tremendously low, issuing a session_start() on any script that uses $_SESSION makes sense.

cineweekly.com
01-25-2012, 05:52 AM
Since I'm super new to php could you guide me on how to fit the session code into the code I already have? After experimenting, I'm guessing it's more than simply putting session_start(); in the php.

Fou-Lu
01-25-2012, 03:40 PM
No, its not. All you need is session_start() at the top of your page, and $_SESSION will now exist and can be populated and read from.

cineweekly.com
01-26-2012, 12:02 AM
When I do that, both with and without the semi-colon, I get these errors:

PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /public_html/index.html:6) in /head.html on line 2

PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /public_html/index.html:6) in /head.html on line 2

Remember, the random link still works fine, these errors only appear in my error_log.

tracknut
01-26-2012, 12:13 AM
You need the session_start at the very top, or at least before any output to the browser (ie, before DOCTYPE, or any of that stuff).

Dave

cineweekly.com
01-26-2012, 12:25 AM
You need the session_start at the very top, or at least before any output to the browser (ie, before DOCTYPE, or any of that stuff).

Dave

Oh, wow, I think that did it, thanks guys. Now I need to find a way to easily insert it into all my pages. The topmost "php require" that I have is for content in the <head>

tracknut
01-26-2012, 12:37 AM
What all do you have in your html file between this session_start and your included header? Can you not stick all of it inside the included header file, so the top of your code looks something like:


<?php include ('head.inc'); ?>

<body>
...blah blah

cineweekly.com
01-26-2012, 01:41 AM
I think my only option is to use Find and Replace <!DOCTYPE with

<?php
session_start();
?>
<!DOCTYPE

Here's the top of every individual page. I would be able to shove more of it (CSS link, etc.) into the head.html if I set up a php testing server. But right now I don't have one so I need the doctype, css, and all that on the page so I can see it properly formatted when writing articles.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CineWeekly: The Outsiders Movie Review</title>
<link href="../core-style.css" rel="stylesheet" type="text/css" />
<?php require("../head.html"); ?>



Also, would placing session_start() on every page cause a new session to start or clear the old one? I've never used sessions before.

Fou-Lu
01-26-2012, 04:42 AM
Very first line. Ensure that you have no whitespace either.
The error you have tells you were your output has started:

PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /public_html/index.html:6) in /head.html on line 2

So with this, your call is:


<?php
include 'index.html'; // or require or whatever
session_start();

And on line 6 of index.html you have explicit output from either a PHP close tag or a print.
Move session_start() to the first line.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum