Forum Moderators: coopster

Message Too Old, No Replies

Perfect PHP Comments script works, but only on local Apache!

It's perfect except for the fact that it doesn't work on my live site :-\

         

JAB Creations

6:34 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some of you may know of my long search for a decent PHP comments script; well I've finally found it. Only one problem, while it works I don't want everyone visiting my site slowing my personal internet connection! ;)

Honestly I have no clue why it's not working; I've adjusted the error level but nothing helpful to the extent I understand. So honestly I do not know what the problem is other then different version of PHP (this script is copyright 2003) and I'm running Apache 2 and my server is 1.3x.

PHP Error reference...
[us2.php.net...]

This was built by Hitech Scripts and not really modded except for the HTML in the form (gah, tables!). Anyway recent posts about finding comments on a page...got that somewhere embeded in this script!

So it looks for the start and end HTML comments to know where to post new comments.

With a very small adjustment I could set the file variable as the referer and have this script work with an unlimited number of pages (which is why I wanted this instead of Matt Wrights modded guest book script since I don't know perl for jack compared to what little I know with PHP).

Anyway if anyone has any clue or wants me to look and see if something is disabled on my phpinfo page just let me know.

The Form!

<form action="cm.php" METHOD="POST">
<input name="action" type="hidden" value="post">
Your name
<input name="name" size="50" type="text" value="">

Email
<input name="email" size="50" type="text" value="">

Site url
<input name="site_url" size="50" type="text" value="">

Location
<input name="location" size="50" type="text" value="">
Comments<textarea name="comments" rows="8" cols="38"></textarea>

<input value="Sign" type="submit">
<input type="reset">
</form>

cm.php

<?php
$filename = "/comments.html";
$store_ip = 0;
error_reporting(8);

if (($action == "add_comment") ¦¦ ($action == "post")) {
$header = get_header();
echo $header;
}

if ($action == "add_comment")
{
$form_text = get_form();
echo $form_text;
}

if ($action == "post")
{
$date = date ("l dS of F Y h:i:s A");

$name = htmlentities($name);

if ($site_url!= "") {
$site_url = htmlentities($site_url);
$url_checker = explode("http://", $site_url);

if (!IsSet($url_checker[1])) {
$site_url = "http://$site_url";
} else {
$site_url = "$site_url";
}
}
else {
$site_url = "none";
}

if ($site_url <> "none") {
$name = '<a target=_blank href="' . $site_url . '">' . $name . "</a>";
}

if ($email!= "") {
$email = htmlentities($email);
$email_checker = explode("@", $email);
if (!IsSet($email_checker[1])) {
$email = "none";
} else {
//email checked
}
}

$ip_address = getIP();
$location = htmlentities($location);

$comments = htmlentities($comments);
$comments = parse_smiles($comments, array(':)' => "biggrin-c.gif", ':?' => "confused-c.gif", 'B)' => "cool-c.gif", ':(' => "cry.gif", 'F:' => "frown-c.gif", 'D)' => "shiny.gif", ')D' => "smile-c.gif", ';)' => "wink-c.gif"));

if (is_writable($filename)) {
$file = @fopen($filename, "r");
$result = fread($file, filesize($filename));
fclose($file);
}
else {
$footer = get_footer();

$file = @fopen($filename, "w");
$cm_text = $header . '<!--Comments_start--> <!--Comments_end-->' . $footer;
fwrite($file, $cm_text);
fclose($file);
chmod ($filename, 0775);
$result = $guest_text;
}

$checker = explode("<!--Comments_start-->", $result);
$footer = get_add();

$msg = get_message($date, $name, $email, $ip_address, $location, $comments);
$cm_text = $checker[0] . '<!--Comments_start-->' . $msg . $checker[1];
$cm_text = eregi_replace($footer, '', $cm_text);

$checker = explode("<!--Comments_end-->", $cm_text);
$cm_text = $checker[0] . $footer . '<!--Comments_end-->' . $checker[1];

$file = @fopen($filename, "w");
fwrite($file, $cm_text);
fclose($file);
chmod ($filename, 0775);

echo $msg;
}

if (($action!= "sign") && ($action!= "post")) {
if (is_writable($filename)) {
$file = @fopen($filename, "r");
$result = fread($file, filesize($filename));
fclose($file);

echo $result;
}
exit;
}

$footer = get_footer();
echo $footer;

exit;
//-------------------------------------------------------------------------------------------------
function get_message($date, $name, $email, $ip_address, $location, $comments) {
$msg = '<!-- IP address - ' . $ip_address . ' -->
<div align="left">
<br>
<br>
<table width="600">
<tr>
<td width="100"><b>Name:</b></td>
<td>' . $name . '</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>' . $email . '</td>
</tr>
<tr>
<td><b>Location:</b></td>
<td>' . $location . '</td>
</tr>
<tr>
<td><b>Date:</b></td>
<td>' . $date . '</td>
</tr>
<tr>
<td colspan="2"><b>Comments:</b></td>
</tr>
<tr>
<td colspan="2" align="left">' . $comments . '</td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
</table>
</div>';

return $msg;
}
//-------------------------------------------------------------------------------------------------
function get_form() {
$form_text = '<form action="guestbook.php" METHOD="POST">
<input name="action" type="hidden" value="post">
<table>
<tr>
<td>Your name</td>
<td><input name="name" size="50" type="text" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" size="50" type="text" value=""></td>
</tr>
<tr>
<td>Site url</td>
<td><input name="site_url" size="50" type="text" value=""></td>
</tr>
<tr>
<td>Location</td>
<td><input name="location" size="50" type="text" value=""></td>
</tr>
<tr>
<td valign="top">Comments</td>
<td><textarea name="comments" rows="8" cols="36"></textarea></td>
</tr>
<tr>
<td>Smiles:</td>
<td align="center">
<table width="260">
<tr>
<td align=center><b>:)</b></td>
<td align=center><b>:?</b></td>
<td align=center><b>B)</b></td>
<td align=center><b>:(</b></td>
<td align=center><b>F:</b></td>
<td align=center><b>D)</b></td>
<td align=center><b>)D</b></td>
<td align=center><b>;)</b></td>
</tr>

<tr>
<td align=center><img src="smiles/biggrin-c.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/confused-c.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/cool-c.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/cry.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/frown-c.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/shiny.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/smile-c.gif" width="17" height="17" border="0"></td>
<td align=center><img src="smiles/wink-c.gif" width="17" height="17" border="0"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" colspan="2"><br><input value="Add comments" type="submit">&nbsp;&nbsp;<input type="reset"></td>
</tr>
</table>
</form>
';

return $form_text;
}
//-------------------------------------------------------------------------------------------------
function get_header() {
$header = '<html>
<head>
<title></title>
<style>
body {font-family:verdana; font-size:8pt}
h1 {font-family:verdana; font-size:12pt}
td {font-size:8pt}

a:link {
color: #333333;
}
a:visited {
color: #666666;
}
a:hover {
color: #CCCCCC;
background-color: #333333;
text-decoration: none;
}
a:active {
color: #333333;
}
.border{border: 1pt solid black}

INPUT {
COLOR: #000000;
BACKGROUND-COLOR: #f6f6f6;
FONT-SIZE: 8pt;
BORDER-BOTTOM: #333333 1px solid;
BORDER-LEFT: #333333 1px solid;
BORDER-RIGHT: #333333 1px solid;
BORDER-TOP: #333333 1px solid
}

SELECT {
COLOR: #000000;
BACKGROUND-COLOR: #f6f6f6;
FONT-SIZE: 8pt;
BORDER-BOTTOM: #333333 1px solid;
BORDER-LEFT: #333333 1px solid;
BORDER-RIGHT: #333333 1px solid;
BORDER-TOP: #333333 1px solid
}

TEXTAREA {
COLOR: #000000;
BACKGROUND-COLOR: #f6f6f6;
FONT-SIZE: 8pt;
BORDER-BOTTOM: #333333 1px solid;
BORDER-LEFT: #333333 1px solid;
BORDER-RIGHT: #333333 1px solid;
BORDER-TOP: #333333 1px solid
}
</style>
</head>

<body>
<center><h1></h1>
<table width="250">
<tr>
<td align=center><a href="' . $filename . '">Back</a></td>
</tr>
</table>

';

return $header;
}
//-------------------------------------------------------------------------------------------------
function get_footer() {
$footer = '</center> <center>Powered by <a href="http://www.hitech-scripts.com">Hitech-scripts.com</a></center></body></html>';

return $footer;
}
//-------------------------------------------------------------------------------------------------
function get_add() {
$footer = '<div align="left"><br><br>Powered by <a href="http://www.hitech-scripts.com">Hitech-scripts.com</a></div>';

return $footer;
}
//-------------------------------------------------------------------------------------------------
function parse_smiles($msg, $parse_string='nil')
{
if (is_array($parse_string))
{
while (list($name, $parse_value) = each ($parse_string))
{
$parse_value1 = '<img src="smiles/'. $parse_value . '" width="17" height="17" border="0">';
$msg = str_replace($name, $parse_value1, $msg);
}
}

return $msg;
}
//-------------------------------------------------------------------------------------------------
function getIP() {
global $store_ip;

$ip;
if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
else $ip = "UNKNOWN";

if ($store_ip == 0) { $ip = "unknown"; }
return $ip;
}
//-------------------------------------------------------------------------------------------------

?>

John

SeanW

9:04 pm on Mar 9, 2006 (gmt 0)

10+ Year Member



$filename = "/comments.html";
...
$file = @fopen($filename, "r");

It's trying to open a file on the root of your hard drive, chances are the file in question exsts somewhere else.

Try removing the @ in front of the fopens and see if you get an error on screen. Or, check the error log of your web server, I'm sure there's a complaint about opening the file.

Sean