PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 5:48 am
Hi there
I have a webhotel which has registered globals turned off and
safety mode turned on. I get errors in this script. What can
I do to correct it?
I get errors in in linie 12..........
Admin Edit: added tags to code - use these tags, it makes your code easier to read and thus you help us help you![/color][/b]
Code: Select all
<?php
function head() {
include('config.php');
echo "<center><table width="200" border="0" cellpadding="1" cellspacing="1">
<tr><td bgcolor="$tableborder">
<table width="198" border="0" cellpadding="0" cellspacing="" align="center">
<tr><td bgcolor="$bgcolor">";
}
function foot () {
echo "</td></tr></table></td></tr></table>";
}
function record($_SERVER['REMOTE_ADDR']) {
include('config.php');
$fp=fopen("$ip_file", "a+");
fputs ($fp,$_SERVER['REMOTE_ADDR'].[".time()."\n");
fclose($fp);
}
function check($REMOTE_ADDR) {
include('config.php');
global $valid;
$ip=$_SERVER['REMOTE_ADDR'];
$data=file("$ip_file");
$now=time();
foreach ($data as $record) {
$subdata=explode("][",$record);
if ($now < ($subdata[1]+1*$timeout)) {
if ($ip == $subdata[0]) {
$valid=0;
break;
}}}}
//##################### Save data ##############################
function save($answer){
global $answer;
include('config.php');
$data=file($datafile);
$subdata=explode("][",$data[$answer]);
$subdata[2]+=1;
$data[$answer]=implode("][", $subdata);
$data[$answer]=$data[$answer]."\n";
$fp=fopen($datafile,"w+");
$a=0;
do{
fputs($fp,$data[$a]);
$a++;
}while($a<count($data));
fclose($fp);
}
//########################## Show Form ############################
function form($PHP_SELF){
include('config.php');
head();
echo "<font size="$fontsize" face="$font" color="$textcolor"><form method="post" action="$PHP_SELF"><p align="center">";
$data=file($datafile);
$question=$data[0];
$nb_options=count($data)-1;
echo "<b>$question</b></p>";
for($nb=1;$nb <= $nb_options; $nb++){
$option=explode("][","$data[$nb]");
echo "<input type="radio" name="answer" value="$nb"> ";
echo "$option[0]<br>";
}
echo "<input type="hidden" name="save" value="yes">";
echo "<p align="center"><input type="submit" name="Submit" value="Send"></form></font>";
echo "<font size="$fontsize" face="$font" color="$textcolor"><a href=""; echo $PHP_SELF; echo "?action=results">Vis mig resultaterne.</font></p>";
foot();
}
//################## Function to show results ########################
function results(){
include('config.php');
head();
$data=file($datafile);
$nb_answers=count($data);
$votes=0;
$a=1;
do{
$subdata=explode("][",$data[$a]);
$votes += $subdata[2];
$a++;
}while($a < $nb_answers);
$a=1;
$b="answerv";
$v=100/$votes;
echo "<p align="center"><font size="$fontsize" face="$font" color="$textcolor"><b>$data[0] ($votes Stemmer.)</b><br><br>";
do{
$subdata=explode("][",$data[$a]);
$av = $subdata[2] * $v;
echo "<font size="$fontsize" face="$font" color="$textcolor">$subdata[0] -"; printf(" %01.1f", $av); echo"%<br>";
$p2v = 100-$av;
echo "<img src="$subdata[1]" width="$av" height="10"><img src="$image" width="$p2v" height="10"><br>";
$a++;
} while ($a < $nb_answers);
echo "<br>";
echo "<br><center><a href="javascript:history.go(-1)">Gå Tilbage.</a><br>\n";
foot();
}
//###################### Engine ####################################
check($REMOTE_ADDR);
if ($valid=="0"){ results();}
elseif ($action=="results") { results(); }
elseif ($save=="yes" && $valid!="0") { save($Answer); record($REMOTE_ADDR); results();}
elseif ($action=="save" && !empty($valid)) { results(); }
elseif ($action!="save" && $valid!="0"){ form($PHP_SELF); }
?>
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Apr 02, 2004 6:17 am
And the error is?
Mac
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 6:21 am
Parse error: parse error, expecting `')'' in /var/www/epoxy/web2/icebdk/docs/vote/vote.php on line 12
Pozor
Forum Commoner
Posts: 74 Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland
Post
by Pozor » Fri Apr 02, 2004 7:11 am
Hello,
somethings wrong with this line:
Code: Select all
<?php
//edit
fputs ($fp,$_SERVER['REMOTE_ADDR']);
?>
Why do you use the attribute lenght? its not necessary! your string is limited! and why use time()?
greez pozor
Pozor
Forum Commoner
Posts: 74 Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland
Post
by Pozor » Fri Apr 02, 2004 7:22 am
hello,
when you want to use the attribute lenght:
Code: Select all
<?php
fputs ($fp,$_SERVER['REMOTE_ADDR'],strlen($_SERVE['REMOTE_ADDR']));
?>
greez Pozor
PS: the result is the same...
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Apr 02, 2004 7:24 am
First error is here:
Code: Select all
function record($_SERVER['REMOTE_ADDR']) {
$_SERVER['REMOTE_ADDR'] is not a valid name for a function attribute. Judging by the function you should just have:
Other issues come to light once that is fixed. The line that Pozer offered a fix for:
Code: Select all
fputs ($fp,$_SERVER['REMOTE_ADDR'].[".time()."n");
plus an unescaped double quote in a double quoted string further down the page.
Mac
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 7:35 am
I did edit that line when I changed server/webhotel....
The original was like this
fputs ($fp,$REMOTE_ADDR."][".time()."\n");
Does it look correct?
Pozor
Forum Commoner
Posts: 74 Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland
Post
by Pozor » Fri Apr 02, 2004 7:39 am
Hello,
use php tags please.
sorry maybe its not so polite: do you really know what you're doing?
look at the PHP manual please:
[php_man]fputs[/php_man]
[php_man]time[/php_man] -> it has no connection to a certain lenght, its a unix timestamp!
[php_man]strlen[/php_man]
For me, it looks like: nonsens programming (the current line -> argument lenght)
greez Pozor
Last edited by
Pozor on Fri Apr 02, 2004 7:41 am, edited 1 time in total.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Apr 02, 2004 7:39 am
It should have been changed to:
Code: Select all
fputs ($fp,$_SERVER['REMOTE_ADDR']."][".time()."\n");
Mac
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 7:39 am
hi
There is still an error here
Parse error: parse error, expecting `')'' in /var/www/epoxy/web2/icebdk/docs/vote/vote.php on line 127
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Apr 02, 2004 7:40 am
Pozor wrote: For me, it looks like: nonsens programming (the current line -> argument lenght)
From my reading it looks like a log file - logging the user's IP and the UNIX timestamp of the access time - maybe not incredibly useful but not nonsense programming.
Mac
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Apr 02, 2004 7:42 am
iceb wrote: hi
There is still an error here
Parse error: parse error, expecting `')'' in /var/www/epoxy/web2/icebdk/docs/vote/vote.php on line 127
I copied and pasted the code and don't have a line 127? Could you post lines 120-130 (and use the
tags around the code).
Mac
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 7:44 am
ok now there are no errors but there is an explanation when
you click on the link
"vis mig resultaterne"
it is supposed to show the results of the vote.
It does not ? Why ? Is it something that is not beeing parsed?
Pozor
Forum Commoner
Posts: 74 Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland
Post
by Pozor » Fri Apr 02, 2004 7:47 am
Hello,
Ok yes this is a good point.
sorry i was confused by the ][...
damn!
sometimes i'm reading to fast...
greez Pozor
iceb
Forum Newbie
Posts: 17 Joined: Mon Oct 28, 2002 6:18 am
Post
by iceb » Fri Apr 02, 2004 7:52 am
ok here is the whole corrected code
but why does the link not work ??
It should show the results of the vote
CODE:
<?php
function head() {
include('config.php');
echo "<center><table width=\"200\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\">
<tr><td bgcolor=\"$tableborder\">
<table width=\"198\" border=\"0\" cellpadding=\"0\" cellspacing=\"\" align=\"center\">
<tr><td bgcolor=\"$bgcolor\">";
}
function foot () {
echo "</td></tr></table></td></tr></table>";
}
function record() {
include('config.php');
$fp=fopen("$ip_file", "a+");
fputs ($fp,$REMOTE_ADDR."][".time()."\n");
fclose($fp);
}
function check($REMOTE_ADDR) {
include('config.php');
global $valid;
$ip=$_SERVER['REMOTE_ADDR'];
$data=file("$ip_file");
$now=time();
foreach ($data as $record) {
$subdata=explode("][",$record);
if ($now < ($subdata[1]+1*$timeout)) {
if ($ip == $subdata[0]) {
$valid=0;
break;
}}}}
//##################### Save data ##############################
function save($answer){
global $answer;
include('config.php');
$data=file($datafile);
$subdata=explode("][",$data[$answer]);
$subdata[2]+=1;
$data[$answer]=implode("][", $subdata);
$data[$answer]=$data[$answer]."\n";
$fp=fopen($datafile,"w+");
$a=0;
do{
fputs($fp,$data[$a]);
$a++;
}while($a<count($data));
fclose($fp);
}
//########################## Show Form ############################
function form($PHP_SELF){
include('config.php');
head();
echo "<font size=\"$fontsize\" face=\"$font\" color=\"$textcolor\"><form method=\"post\" action=\"$PHP_SELF\"><p align=\"center\">";
$data=file($datafile);
$question=$data[0];
$nb_options=count($data)-1;
echo "<b>$question</b></p>";
for($nb=1;$nb <= $nb_options; $nb++){
$option=explode("][","$data[$nb]");
echo "<input type=\"radio\" name=\"answer\" value=\"$nb\"> ";
echo "$option[0]<br>";
}
echo "<input type=\"hidden\" name=\"save\" value=\"yes\">";
echo "<p align=\"center\"><input type=\"submit\" name=\"Submit\" value=\"Send\"></form></font>";
echo "<font size=\"$fontsize\" face=\"$font\" color=\"$textcolor\"><a href=\""; echo $PHP_SELF; echo "?action=results\">Vis mig resultaterne.</font></p>";
foot();
}
//################## Function to show results ########################
function results(){
include('config.php');
head();
$data=file($datafile);
$nb_answers=count($data);
$votes=0;
$a=1;
do{
$subdata=explode("][",$data[$a]);
$votes += $subdata[2];
$a++;
}while($a < $nb_answers);
$a=1;
$b="answerv";
$v=100/$votes;
echo "<p align=\"center\"><font size=\"$fontsize\" face=\"$font\" color=\"$textcolor\"><b>$data[0] ($votes Stemmer.)</b><br><br>";
do{
$subdata=explode("][",$data[$a]);
$av = $subdata[2] * $v;
echo "<font size=\"$fontsize\" face=\"$font\" color=\"$textcolor\">$subdata[0] -"; printf(" %01.1f", $av); echo"%<br>";
$p2v = 100-$av;
echo "<img src=\"$subdata[1]\" width=\"$av\" height=\"10\"><img src=\"$image\" width=\"$p2v\" height=\"10\"><br>";
$a++;
} while ($a < $nb_answers);
echo "<br>";
echo "<br><center><a href=\"javascript:history.go(-1)\">Gå Tilbage.</a><br>\n";
foot();
}
//###################### Engine ####################################
check($REMOTE_ADDR);
if ($valid=="0"){ results();}
elseif ($action=="results") { results(); }
elseif ($save=="yes" && $valid!="0") { save($Answer); record($REMOTE_ADDR); results();}
elseif ($action=="save" && !empty($valid)) { results(); }
elseif ($action!="save" && $valid!="0"){ form($PHP_SELF); }
?>