Webspace & Domain Names This is a discussion on, new platform and php cgi files within the Freedom2Surf forum; How do we use php to write to a file now? Back in the good 'ole days at f2s we'd ...

Reply
 
LinkBack Thread Tools
Old 21-08-2007, 10:49 PM   #1
Tiscali User Member
 
php4ever's Avatar
 
Join Date: Jul 2002
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
new platform and php cgi files

How do we use php to write to a file now? Back in the good 'ole days at f2s we'd just create a php source file like a perl script and give it a .cgi suffix. I created a few files like that which simply write data to another file. None of those scripts are working now. Anyone have any debugging hints or help?
I'd greatly appreciate it.

Many thanks.

php4ever

p.s. Note: I previously asked if the new platform was going to use fastcgi and was told no way here. However, I checked my phpinfo output and sure enough f2s is using fastcgi. So, do I have to do s.thing extra or different to make sure my php/cgi scripts run?
php4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 22-08-2007, 09:20 AM   #2
Former Freedom2Surf Staff
 
Join Date: May 2006
Posts: 272
Thanks: 0
Thanked 0 Times in 0 Posts
How are you ascertaining that it's fastcgi? As far as the platform team are concerned it's using straightforward cgi.

More detail required on running your PHP - how are you invoking the script?
richard_f2s is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 22-08-2007, 09:36 AM   #3
Former Freedom2Surf Staff
 
Join Date: May 2006
Posts: 272
Thanks: 0
Thanked 0 Times in 0 Posts
Further clarification - although phpinfo does indeed report enable-fastcgi it's implemented on the platform.
richard_f2s is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 22-08-2007, 06:23 PM   #4
Tiscali User Member
 
php4ever's Avatar
 
Join Date: Jul 2002
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Why won't this work anymore:

#!/usr/bin/php


<?php
error_reporting(E_ALL);

$id = "poll032502"; // poll i.d.
// check to ensure that the form has been submitted
if ( isset( $_POST['poll_taker'] ) ) {
if (!$_POST['submit'] || !isset($_POST['poll_ans'])) {
?>
<html>
<head><title>Error Page</title>
<basefont face="Arial,Helvetica,Geneva">
<LINK REL="stylesheet" TYPE="text/css" HREF="poll_style.css">
</head>

<body bgcolor="#ffffe0" scroll="auto">
<!-- ERROR PAGE -->
<div align="center">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<font size="+1"><i><span style="color:red">Error!</span> Please <a href="<?php echo $_SERVER['PHP_SELF'] . '?goback=okay'; ?>">try again</a></i></font>
</div>
<?php
exit();
}
// check the cookie to ensure that user has not voted already
else if ($_COOKIE['lastpoll'] && $_COOKIE['lastpoll'] == $id) {
?>
<html>
<head><title>Already Voted Page</title>
<!-- ALREADY VOTED -->
<basefont face="Arial,Helvetica,Geneva">
<LINK REL="stylesheet" TYPE="text/css" HREF="poll_style.css">
<script language="JavaScript">
<!-- prevent user from coming back, unless returning fr error page.
// comment out below js to disable.
if (location.href == '<?php echo $_SERVER["SCRIPT_URI"] ?>' )
window.history.forward(1);
//-->
</script>
</head>

<body bgcolor="#ffffe0">

<?php
$whichfile="$DOCUMENT_ROOT_REAL/wrts_poll.txt";
$fp=0;

$fp = @fopen($whichfile,'r');
if (!$fp) { die("unable to open $whichfile for reading."); }

$contents = fread($fp, filesize($whichfile));
fclose($fp);

list($votes1,$votes2,$votes3,$votes4,$votes5) = explode("\n",$contents);

// count the total votes
$total = $votes1 + $votes2 + $votes3 + $votes4 + $votes5;

// calculate each as a percentage of the total, round to two decimals
$perc_votes1 = round(($votes1/$total)*100,2);
$perc_votes2 = round(($votes2/$total)*100,2);
$perc_votes3 = round(($votes3/$total)*100,2);
$perc_votes4 = round(($votes4/$total)*100,2);
$perc_votes5 = round(($votes5/$total)*100,2);



// print it all in a neat table

echo "<div align='center'>\n";
echo "<table border='0' cellspacing='0' cellpadding='6'>\n";
echo "<tr>\n";
echo "<th colspan=3><font color='midnightblue'>Are Workers Rights the Foundation of Democracy?</font></th>\n";
echo "</tr>\n";


// also display an image graph
echo "<tr valign='top'>\n<td><font size=-2>Strongly Agree</font></td><td><font size=-2> $votes1 &nbsp;( $perc_votes1% )</font></td>";
echo "<td rowspan=5><img src='exp_graph.php?votes1=$votes1&votes2=$votes2&v otes3=$votes3&votes4=$votes4&votes5=$votes5' width='200px' height='140px' border='0'>
</td>\n</tr>\n";
echo "<tr valign='top'>\n<td><font size=-2>Strongly Disagree</font></td><td><font size=-2> $votes2 &nbsp;( $perc_votes2% )</font></td>\n</tr>\n";
echo "<tr valign='top'>\n<td><font size=-2>Unsure</font></td><td><font size=-2> $votes3 &nbsp;( $perc_votes3% )</font></td>\n</tr>\n";
echo "<tr valign='top'>\n<td><font size=-2>Don't Want to Know</font></td><td><font size=-2> $votes4 &nbsp;( $perc_votes4% ) </font></td>\n</tr>\n";
echo "<tr valign='top'>\n<td><font size=-2>Stop! My Brain Hurts!</font></td><td><font size=-2> $votes5 &nbsp;( $perc_votes5% ) </font></td>\n</tr>\n";
echo "<tr valign='top'>\n<td align='right'><font size=-2>TOTAL</font></td><td align='left'><font size=-2> $total votes</font></td></tr>";
echo "\n</table>\n</div>";

?>
<font size="-2"><i>You have already voted once. Come back in a few days for another poll,
or <a href="#">you will soon be able to click here</a> to view previous polls</i></font>
<?php
}
// all is well - refresh the cookie (or set a new one) and process the vote
else
{
setCookie("lastpoll", $id, time()+(60 * 60 * 24));
?>
<html>
<head>
<title>Workers Rights Poll: Responses</title>
<basefont face="Arial,Helvetica,Geneva">
<LINK REL="stylesheet" TYPE="text/css" HREF="poll_style.css">
</head>
<body bgcolor="#ffffe0" scroll="auto">
<!-- RESPONSES -->
<?php
include("Lockfile.inc"); // wrapper functions to lock and unlock file

// corresponding to which choice user makes.
$votes1=0;
$votes2=0;
$votes3=0;
$votes4=0;
$votes5=0;

$whichfile="$DOCUMENT_ROOT_REAL/wrts_poll.txt";
$fp=0;

$fp = @fopen($whichfile,'r');
if (!$fp) { die("unable to open $whichfile for reading."); }

$contents = fread($fp, filesize($whichfile));
fclose($fp);

list($votes1,$votes2,$votes3,$votes4,$votes5) = explode("\n",$contents);

$answer = ($_POST['poll_ans'] > -1 && $_POST['poll_ans'] < 5)? $_POST['poll_ans'] : -1;
if ($answer == -1) die("bad data");


// can I replace w/case construct in php or an associate array?
{
if ($answer == 0) $votes1++;
else
if ($answer == 1) $votes2++;
else
if ($answer == 2) $votes3++;
else
if ($answer == 3) $votes4++;
else
if ($answer == 4) $votes5++;
}


// count the total votes
$total = $votes1 + $votes2 + $votes3 + $votes4 + $votes5;

// calculate each as a percentage of the total, round to two decimals
$perc_votes1 = round(($votes1/$total)*100,2);
$perc_votes2 = round(($votes2/$total)*100,2);
$perc_votes3 = round(($votes3/$total)*100,2);
$perc_votes4 = round(($votes4/$total)*100,2);
$perc_votes5 = round(($votes5/$total)*100,2);


print <<<END
<div align="center">
<table border='0' cellspacing='0' cellpadding='6'>
<tr>
<th colspan=3><font color='midnightblue'>Are Workers Rights the Foundation of Democracy?</font></th>
</tr>

<tr valign='top'>
<td><font size=-2>Strongly Agree</font></td><td><font size=-2> $votes1 &nbsp;( $perc_votes1% )</font></td>
<td rowspan=5><img src="exp_graph.php?votes1=$votes1&votes2=$votes2&v otes3=$votes3&votes4=$votes4&votes5=$votes5" width='200px' height='140px' border='0'>
</td>
</tr>
<tr valign='top'>
<td><font size=-2>Strongly Disagree</font></td><td><font size=-2> $votes2 &nbsp;( $perc_votes2% )</font></td>
</tr>
<tr valign='top'>
<td><font size=-2>Unsure</font></td><td><font size=-2> $votes3 &nbsp;( $perc_votes3% )</font></td>
</tr>
<tr valign='top'>
<td><font size=-2>Don't Want to Know</font></td><td><font size=-2> $votes4 &nbsp;( $perc_votes4% ) </font></td>
</tr>
<tr valign='top'>
<td><font size=-2>Stop! My Brain Hurts!</font></td><td><font size=-2> $votes5 &nbsp;( $perc_votes5% ) </font></td>
</tr>
<tr valign='top'>
<td align='right'><font size=-2>TOTAL</font></td><td align='left'><font size=-2> $total votes</font></td>
</tr>
</table>
</div>
END;
// save the votes
$a = array();
$a[0] = $votes1;
$a[1] = $votes2;
$a[2] = $votes3;
$a[3] = $votes4;
$a[4] = $votes5;

$contents = implode("\n",$a);

$fp = fopen($whichfile, "w");
if (!$fp) { die("unable to open $whichfile for writing."); }

// lock file so no other processes can access it at same time
lock($fp);

fputs($fp,$contents);

unlock($fp);

@fclose($fp);
} // end process the vote code

} else { // if form not submitted, then display form
?>
<html>
<head>
<title>Workers Rights Poll: Choices</title>
<basefont face="Arial,Helvetica,Geneva">
<LINK REL="stylesheet" TYPE="text/css" HREF="poll_style.css">

<script>
var radbut_clicked=false;

function Valid(){
if (radbut_clicked) return true;
else {
alert("You need to make a selection and then you may vote.");
return false;
}
}
function Lightup(but){
but.className="pushbut_on";
}
function LightPink(but) {
but.className="pushbut_pink";
}

function Norm(but){
but.className="pushbut";

}
function ChoiceMade(form){
radbut_clicked=true;
form.submit.focus()
}
</script>
</head>
<body bgcolor="#ffffe0" scroll="auto">
<!-- CHOICES -->
<div align="center"><font color="midnightblue" face="Arial,Helvetica,Geneva"><b>Are Workers' Rights the Foundation of Democracy?</b></font>
<!--form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="myform" onsubmit="return Valid();"-->
<form action="<?php echo $_SERVER['SCRIPT_URL']; ?>" method="post" name="myform" onsubmit="return Valid();">
<table border=0 cellspacing=2 cellpadding=2>
<tr valign=middle><td><input type=radio name="poll_ans" onclick="ChoiceMade(this.form)" value=0></td><td> <font face=Arial,Helvetica,Geneva><font size="-1">Strongly Agree</font></font></td></tr>
<tr valign=middle><td><input type=radio name="poll_ans" onclick="ChoiceMade(this.form)" value=1></td><td> <font face=Arial,Helvetica,Geneva><font size="-1">Strongly Disagree</font></font></td></tr>
<tr valign=middle><td><input type=radio name="poll_ans" onclick="ChoiceMade(this.form)" value=2></td><td> <font face=Arial,Helvetica,Geneva><font size="-1">Unsure</font></font></td></tr>
<tr valign=middle><td><input type=radio name="poll_ans" onclick="ChoiceMade(this.form)" value=3></td><td> <font face=Arial,Helvetica,Geneva><font size="-1">Don't Want to Know</font></font></td></tr>
<tr valign=middle><td><input type=radio name="poll_ans" onclick="ChoiceMade(this.form)" value=4></td><td> <font face=Arial,Helvetica,Geneva><font size="-1">Stop! My Brain Hurts!</font></font></td>
</tr>
</table>
<input type=hidden name="poll_taker" value=1>
<br><INPUT TYPE="submit" name="submit" class="pushbut" onmouseover="Lightup(this)" onmouseout="Norm(this)" VALUE=" Vote ">&nbsp;
<input type=reset class="pushbut" onmouseover="LightPink(this)" onmouseout="Norm(this)" onclick="radbut_clicked=false;" value=" Clear ">
</form>
</div>
<?php
} // end display form
?>
</body>
</html>
php4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 22-08-2007, 07:58 PM   #5
Tiscali User Member
 
Join Date: Feb 2006
Posts: 388
Thanks: 0
Thanked 0 Times in 0 Posts
Without looking into it too much, i might suggest looking at the line:

$whichfile="$DOCUMENT_ROOT_REAL/wrts_poll.txt";

Think i saw somewhere that register_globals was now off, in which case you'll get to use the superglobal for the docroot.
__________________
ex-f2s - Now on Be Unlimited
Sync: 7779kbps (859KB/s throughput) / 1295kbps (137KB/s throughput)
View my ADSL Statistics - See my gripe with Webfusion
elrandom3 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 23-08-2007, 08:40 PM   #6
Tiscali User Member
 
php4ever's Avatar
 
Join Date: Jul 2002
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
I think elrandom3's comment above is excellent and I will check into it and see if that solves things.

Two things to keep in mind if a site doesn't work rt now is whether or not it's a php4 vs php5 issue or whether it's really the new platform. I discovered that another one of my scripts had issues of both types. The main php issue was that the fake GETS were not working b/c PHP5 has the filter extension turned on by default. Once I treated the query string I had affixed to a url in code as a query string then the php behaved and cranked out the expected html.

Yep, I saw the html which turned out to be precisely the next problem. I saw the html instead of seeing the browser's interpretation of it. Grr! In the 'ole days at f2s you couldn't write to a file using php unless your script had a .cgi extension. Now with the new platform that .cgi extension is a problem. Once I renamed the script with a .php extension the script worked like a charm.

Another happy thought to keep in mind is if you see a 500 error you may have an error in your code or it could be a permissions error.

I think it would have been far better had the powers that be over at f2s introduced the 2 major changes sequentially. Many users may become frustrated and upset having to figure out what and which type of problem(s) their sites may be having and then having to figure out how to fix these.
php4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
f2s Webserver - New Platform Migration (Update) richard_f2s Webspace & Domain Names 22 22-08-2007 07:57 PM
Enlightenment ... php4ever Webspace & Domain Names 3 21-05-2007 11:21 AM
New server: PHP or FP extensions NOT BOTH kcider Webspace & Domain Names 5 09-02-2007 09:37 AM
New Platform - Changes Samizdata Webspace & Domain Names 37 07-02-2007 07:06 AM


All times are GMT. The time now is 06:28 AM.