Solved

Need help converting a CGI script to PHP

Posted on 2002-03-08
8
225 Views
Last Modified: 2006-11-17
well, my free web host died on me. i moved to a new one and it allows PHP scripts but for CGI scripts to work you'd have to pay $$.

i figured ok, i know a little perl, how hard could this php thing be? for my form mail scripts, i just downloaded a ready made version (so much for learning).

but, my other script, it's sorta like a guestbook, but it's not. i tried looking at the php code of guestbook scripts to see if i could figure out how, but that didn't help any, most had something to do with MySQL. then i went to php.net to see if i could look up bits of code... i just got lost.

so here's my little script:

#!/usr/bin/perl

use CGI;

$q=new CGI;
$list ='list.html';
$|++;
print "Content-type: text/html\n\n";

unless (open(LIST, "$list")) {
   print "Could not open $list $!\n";
   die "Could not open $list $!";
}
@contents=<LIST>;
close(LIST);

open (LIST, ">$list");

foreach $line (@contents) {
   print LIST $line;
   $line=~/<!--data-->/ || next;
   print LIST "<CENTER>Date: ", $q->param('date'), "<BR>\n";
   print LIST "Name: ", $q->param('name'), "<BR>\n";
   print LIST "Class: ", $q->param('classtype'), "<BR>\n";
   print LIST "Explaination: ", $q->param('explain'), "<BR>\n";
   print LIST "</CENTER>";
}

close (LIST);

#send them to the list
print qq|<HTML><HEAD><TITLE></TITLE><META HTTP-EQUIV="refresh" CONTENT="0; URL=$list"></HEAD></BODY></HTML>|;

How (appreciate examples, explaination of code etc. :) can I do this using PHP? if not, i guess i'll go look for a CGI enabled host. =\
0
Comment
Question by:sayhi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6852324
From what I understood from the script,

Basically this script processes form and makes a new entry in list.html under <!--data--> section. Right ???

Check the code below,

<?php

$list = 'list.html'; //name of file.

$lines_array = file($list) or die('Can not open list file ' . $list); //file function opens file named $list and each line of the file is put in differnet array elements.

$fp = fopen($list,'w') or die('Can not write to file ' . $list);

$CRLF = "\n"; //keep it "\r\n" if on unix/linux.

flock($fp,LOCK_EX); //lock file for writing.


//following code should be used if your form is submitted by GET method.
// as such if you have register_globals on in your php.ini [ normally it is ] you don't nee to fetch value like this, but this is the best practise.

$date = $HTTP_GET_VARS['date'];
$name = $HTTP_GET_VARS['name'];
$class= $HTTP_GET_VARS['classtype'];

/*
Put up like
$date = $HTTP_POST_VARS['date'];
$name = $HTTP_POST_VARS['name'];
$class= $HTTP_POST_VARS['classtype'];

If your form is submitted by POST Method.

*/

foreach($lines_array as $line)
{
     fwrite($fp,$line . $CRLF); //write original lines as it is.
     if(eregi('\<\!--data--\>',$line)) // if line stats its a place where data is stored
     {
          fwrite("<CENTER>Date: $date<br>" . $CRLF); //write out new entry.
          fwrite("<CENTER>Name: $name<br>".$CRLF);
          fwrite("<CENTER>Class:$classtype<br>".$CRLF);
          fwrite("</CENTRE>" . $CRLF);
     }

}
fclose($fp); //close the file.

?>
<HTML>
     <HEAD>
          <TITLE></TITLE>
          <META HTTP-EQUIV="refresh" CONTENT="0; URL=$list">
     </HEAD>

     <BODY>
     </BODY>
</HTML>



JD
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6852702
Oops,
one thing I forgot,

Just change those last few lines of HTML code to this,

<HTML>
    <HEAD>
         <TITLE></TITLE>
         <META HTTP-EQUIV="refresh" CONTENT="0; URL=<?=$list">
    </HEAD>

    <BODY>
    </BODY>
</HTML>


Rest stays the same,
JD
0
 
LVL 1

Author Comment

by:sayhi
ID: 6854195
" Basically this script processes form and makes a new entry in list.html under <!--data--> section. Right
??? "

yup

Form is submitted by Post.

Stuff I don't understand/can't figure out:

$CRLF = "\n"; //keep it "\r\n" if on unix/linux. (what's this?)

//following code should be used if your form is submitted by GET method.
// as such if you have register_globals on in your php.ini [ normally it is ] you don't nee to fetch
value like this, but this is the best practise. (what's this mean??)

I'll try out your code tonight.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
dkjariwala earned 200 total points
ID: 6856152
Okie,
The carriage return are different for *nix and Windows
Inf windows, New lines are specified by \n whereas on *nix it is separated by \r\n.

Oh one more goof up.

Use following foreach loop instead of previous code.

foreach($lines_array as $line)
{
    fwrite($fp,$line . $CRLF); //write original lines as it is.
    if(eregi('\<\!--data--\>',$line)) // if line stats its a place where data is stored
    {
         fwrite($fp,"<CENTER>Date: $date<br>" . $CRLF); //write out new entry.
         fwrite($fp,"<CENTER>Name: $name<br>".$CRLF);
         fwrite($fp,"<CENTER>Class:$classtype<br>".$CRLF);
         fwrite($fp,"</CENTRE>" . $CRLF);
    }

}
         
See here I have wrote one line ( <CENTER>Date : ***)and then I need to put up new line character. So I am putting value of varialble $CRLF, which you have setup earlier depending upon your os platform.

And regarding that register_globals, don't worry about it right now. Once you study PHP little , you will find out what I am talking about.

JD
0
 
LVL 1

Author Comment

by:sayhi
ID: 6858369
ah okay.

well, i haven't gotten it to work yet.
here is the error i am getting:

Warning: fopen("/usr/home/vhost/username/list.html", "w") - Permission denied in /usr/home/freehost/10023/cgi-bin/list.php on line 8
Can not write to file /usr/home/vhost/username/list.html
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6859498
Error is pretty self explanatory.


The fopen statement is same as open (LIST, ">$list") incase of PHP.

Here the error is,The file you are trying to update is not writable by script,

CHMOD it to 766 an it should work.

JD
0
 
LVL 1

Author Comment

by:sayhi
ID: 6863499
Doh, i totally forgot permissions
i should've know that heh

anyway, THANK YOU so much for your help. now people can start to post stuff at my site.
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6863579
My pleasure !! :)

JD
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question