Page 1 of 1
Hello - a question or two about PHP 5.3 code
Posted: Thu Jul 30, 2009 8:54 pm
by furby_fred
Hello, I am new to this forum
I have done a bit with php previously but not really worked with it in the past few years. When I did so I wwas more accustomed to the ISAPI set up which now it seems has become obsolete.
When I last did any work with PHP OIt was more or less around the time PHP 4 moved up to 5 - I remember having gotten in contact with Larry Ulmann at the time as a lot of the Global Syntax had changed and couldn't work through his books. Even with the pHP titles I bought the code Larry used I am sure was pho 5 but by the time I bought the book the syntax had changed. This seems to be the case with nearly every update to php, not only the syntax but also the configuration...
I now find existing scripts I had, that ran in 5.2.10 on an isapi install only a few days ago are now giving me trouble
Included here is the first snippet:
Code: Select all
<?php
if($process) {
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$message = stripslashes($message);
$origin = "This email originated on the webform at http://www.coup.com";
mail("headless_chikkin@coup.com",$subject,$message,
"From: $name <$email>\r\n" .
"Reply-To: $name <$email>\r\n" .
"X-Mailer: PHP/" . phpversion());
$message = str_replace("\n","<br>",$message);
echo "<h3><font color= #545F1D>Your message has been sent succesfully!!!</font></h3><hr/>";
echo "<b><font color= #660000>From:</font></b> $name <".$email."><br/><br/>";
echo "<b><font color= #660000>Subject:</font></b> $subject<br>";
echo "<hr/><br/><b><font color= #000000>Message:</font></b> <i>$message</I><br/>";
echo "<br/><hr/>";
echo "<b><font color= #660000>Thank You for your comment</font></b>";
}
else
{
echo "<font color= #000000>Sorry but you can't access this page directly</font>";
?>
}
I had a heckuva time setting up 5.3 on a 64bit Vista machine but followed
this very well explained step by step on installing FastCGI PHP and it seems to be running fine except for the fact it is throwing a lot of parser errors.
In the snippet above the script falls down at:
if($process)
The second example should be a lot more straight forward... but for the life of me I cannot figure out why the script is falling down, I have explicitly declared variables (This exercise btw comes from Larry's PHP 5 and MySQL 6 for Dynamic Websites title) and given them valued derived directly from the Posted Values on the Html form. The names I have treble chequed a dozen times.... and still I am getting undefined index error messages.
here is the php end of the script
Code: Select all
<!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>Form Feedback</title>
</head>
<body>
<?php # Script 2.2 - handle_form.php
// Create a shorthand for the form data:
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];
/* Not used:
$_REQUEST[‘age’]
$_REQUEST[‘gender’]
$_REQUEST[‘submit’]
*/
// Print the submitted information:
echo "<p>Thank you, <b>$name</b>, for the following comments:<br />
<tt>$comments</tt></p>
<p>We will reply to you at <i>$email</i>.</p>\n";
?>
</body
again, as mentioned the incoming values posted from the html page are name, email and comments )as written, lower-case)
Can anyone please shed some light on why supposedly very straight-forward scripts are suddenly coming horribly undone on this'latest flavour')
Thanks in advance
- ps forgot to mention I have changed $-REQUEST to $_GET and $_POST nothing makes any difference. I suspect variable definition has once again gotten an overhaul
Re: Hello - a question or two about PHP 5.3 code
Posted: Thu Jul 30, 2009 10:00 pm
by requinix
It'd be easier if you posted the entire script.
While you wait, what's the value of your register_globals INI setting?
Re: Hello - a question or two about PHP 5.3 code
Posted: Thu Jul 30, 2009 11:15 pm
by furby_fred
tasairis wrote:It'd be easier if you posted the entire script.
While you wait, what's the value of your register_globals INI setting?
Hi, and thanks for the speedy reply...
Reg globals was off by default but I switched them on and off to see it it would solve the problem before I posted. No Dice though. I confirmed that they were on each time by restarting IIS7 and then checkhing out my phpoinfo.php
here is the full html side of the code
Code: Select all
<!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>Simple HTML Form</title>
</head>
<!--Script 2.1 Form.html-->
<body>
<form id="form1" name="form1" method="post" action="handle_form.php">
<fieldset><legend>Enter your
information in the form below:</legend>
<p><b>Name:</b> <input type=”text”
name=”name” size=”20” maxlength=”40”
/></p>
<p><b>Email Address:</b> <input
type=”text” name=”email” size=”40”
maxlength=”60” /></p>
<p><b>Gender:</b> <input type=”radio”
name=”gender” value=”M” /> Male <input
type=”radio” name=”gender” value=”F” />
Female</p>
<p><b>Age:</b>
<select name=”age”>
<option value=”0-29”>Under 30</option>
<option value=”30-60”>Between 30 and 60</option>
<option value=”60+”>Over 60</option>
</select></p>
<p><b>Comments:</b> <textarea
name=”comments” rows=”3”
cols=”40”></textarea></p>
</fieldset>
<div align="center">
<input type="submit" name="submit" id="submit" value="Submit My Registration" />
</div>
<div align="center"></div></form>
</body>
</html>
- The full php side
Code: Select all
<!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>Form Feedback</title>
</head>
<body>
<?php # Script 2.2 - handle_form.php
// Create a shorthand for the form data:
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];
/* Not used:
$_REQUEST[‘age’]
$_REQUEST[‘gender’]
$_REQUEST[‘submit’]
*/
// Print the submitted information:
echo "<p>Thank you, <b>$name</b>, for the following comments:<br />
<tt>$comments</tt></p>
<p>We will reply to you at <i>$email</i>.</p>\n";
?>
</body>
</html>
- I have been reading up in 5.3 and it does seem that super globals have been axed in preparation for ver 6.0 which is still 'just around the corner' (and seemingly has been for a number of years now). $_REQUEST in particular represents a big security threat and most likely is no longer supported... so how then does one get variables values passed from a html form if one cannot use them???
Definitely get the feeling I am chasing my tail here and it wouldn't be the first time with php. Hopefully you are up and running on 5.3 and can check out both sides of this equation. Am toying with installing Apache but not sure if it can be configured on 64 bit Vista with the curent version of php. Getting it installed with IIS7 was tricky enough!
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 9:46 am
by redmonkey
I'm not sure about super globals being axed, where did you read that? Are you sure you're not referring to the 'register_globals' ini config option which has been deprecated in PHP 5.3 and will be obsolete in PHP 6?
I may be mistaken but in your first code snippet it looks like your closing brace of the 'if else' statement is after/outside the closing tag of the PHP code block, this may or may not cause a parser error depending on if you have further PHP code blocks (and their content) within that file.
The HTML side of your second snippet may be giving you problems due to your quoting style for attributes. Attribute values should be enclosed in standard quotes, double quotes (") are the more common but single quotes (') are also recognised. I'm not sure what the official name is for the quote style you are using (I can't copy & paste in them as it produces a different quote, they look like the type of quotes you get when you copy and paste from a Word document) but it may be that the HTML parser is not seeing tham as standard quotes and therefore isn't passing the variable names you are expecting if it's passing anything at all.
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 11:25 am
by furby_fred
Hi Redmonkey,
Thanks for your input... I may have confused ithe super global issues, cannot remember exactly where I read it but I googled php 5.3 syntax changes (or similar) and found it in some of the resulting threads.
What I can say is this though that regardless of whether I uses $_REQUEST, $_GET, $_POST I continue to get the undefined index errors. Looks to me as though I defined them. Furthermore the script only falls down on 5.3 IIS7 Vista 64. I tried both scripts again last night on Entropy 5.2.9.7 on Mac and they execute without a bother.
as foe the first script - very astutley spotted. Not sure how that bracket got included. I checked my code and it does not have a bracket outsite the php close tag.
So I will try again.
Here is the comolete php code for problem 1
Code: Select all
<!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>peruquechua :: contact</title>
<style type="text/css">
<!--
table td {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 90%;
font-weight: bold;
background-color: #FFFFFF;
border: thin solid #999999;
}
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
color: #000000;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #FFFFFF;
}
h2 {
color: #660000;
font-family: Georgia, "Times New Roman", Times, serif;
font-weight: bold;
font-size: 200%;
}
-->
</style>
<link href="/peruquechua/html/contentpages.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="content">
<table width="525" height="339" border="0" align="center">
<tr>
<td width="81%"><h2 align="center">SUCCESS/EXITO!!!</h2></td>
<td width="19%"><div align="center"><img src="images/success.gif" alt="success" width="50" height="50" /></div></td>
</tr>
<tr>
<td height="272" colspan="2"><div align="center"> <br />
<table width="80%" >
<tr>
<td># <?php
if($process) {
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$message = stripslashes($message);
$origin = "This email originated on the webform at http://www.coup.com";
mail("headless_chikkin@coup.com",$subject,$message,
"From: $name <$email>\r\n" .
"Reply-To: $name <$email>\r\n" .
"X-Mailer: PHP/" . phpversion());
$message = str_replace("\n","<br>",$message);
echo "<h3><font color= #545F1D>Your message has been sent succesfully!!!</font></h3><hr/>";
echo "<b><font color= #660000>From:</font></b> $name <".$email."><br/><br/>";
echo "<b><font color= #660000>Subject:</font></b> $subject<br>";
echo "<hr/><br/><b><font color= #000000>Message:</font></b> <i>$message</I><br/>";
echo "<br/><hr/>";
echo "<b><font color= #660000>Thank You for your comment</font></b>";
}
else
{
echo "<font color= #000000>Sorry but you can't access this page directly</font>";
?>
<div align="center"></div>
<div align="center"></div></td>
</tr>
</table>
<br />
</div></td>
</tr>
</table>
</div>
</body>
</html
and the cooresponding HTML code for the Web Form
Code: Select all
<!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>peruquechua :: contact</title>
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: medium;
color: #000000;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<link href="/peruquechua/html/contentpages.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="content"><form action="email.php" method="post">
<table width="98%" align="center" >
<tr>
<td height="33"><div align="right">
<div align="center">Your Name/Su Nombre:
<input type="hidden" name="process" value="1">
</div>
</div>
</td>
</tr>
<tr>
<td height="42"><div align="center">
<input type="text" name="name" size="50" maxlength="30">
</div></td>
</tr>
<tr>
<td height="21"><div align="center">Your Email Address/Su Correo:</div></td>
</tr>
<tr>
<td height="34"><div align="right">
<div align="center">
<input type="text" name="email" size="50" maxlength="35">
<br>
</div>
</div>
<div align="center"></div></td>
</tr>
<tr>
<td height="35"><div align="right"></div>
<div align="center">Subject/Tema:</div></td>
</tr>
<tr>
<td height="34"><div align="center">
<input type="text" name="subject" size="70" maxlength="70">
</div></td>
</tr>
<tr>
<td height="45"><div align="center">Message/Mensaje:</div></td>
</tr>
<tr>
<td><div align="center">
<textarea name="message" cols="60" rows="8"></textarea>
</div></td>
</tr>
<tr>
<td height="45"><div align="center">
<input type="submit"style='font:10pt Arial, Helvetica, sans-serif; border:1 none #FFCC00; cursor:hand; background:#000000; color: #FFCC00; ' name="submit"value="Send">
<input type="reset" style='font:10pt Arial, Helvetica, sans-serif; border:1 none #FFCC00; cursor:hand; background:#000000; color: #FFCC00; 'name="Reset"value="Reset">
</div></td>
</tr>
</table>
<table width="160%" >
</table>
<p align="center"> </p>
</form></div>
</body>
</html>
Thanks again...
I will have a look at the quoting style later on... but as mentioned this script has worked fine for a few years now and suddenly a new computer rebuild, upgraded OS to 64 bits and IIS 7 and the latest php distro and I have problems....
previously I used Isapi and on IIS 5 or 6 (WIndows), or apache (Windows or Mac)
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 12:05 pm
by redmonkey
In your complete script for problem 1 I'm not seeing a closing brace at all (unless I've missed it), also $process doesn't appear to be defined prior to checking, it appears that $process is reliant on register_globals being enabled?
To be fair, you haven't just upgraded your PHP installation, you've upgraded your entire environment which could bring other isses into the mix. Your new install of PHP may have come with a new/updated php.ini file, sometimes this file is installed in the correct place and sometimes it isn't so if you're making changes to the php.ini file ensure it's the correct one, perhaps the easiest way of checking that your changes are taking effect is to view the output of phpinfo().
If you don't have a true debugging environment you could temporarily add print_r($_REQUEST); to your script to visually see which variables are being passed to PHP in the first instance. Be aware tho that if those quotes are causing an issue they may be getting converted to a null (or some other non-viewable) character.
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 12:32 pm
by furby_fred
I made sure to intall it to the usual locationC:\php
having gone through the same .ini script I can see that things like the mssql extension is no longer there at all, all extensions are now uncommented etc...
again, how could I test this with reg globals off?
can I simply declare $process as you would in C++ or Java
there being something like
var $process;
but in php simply
$process;
does it need to be initialised? (example? please)
- I am a bit out of touch with coding practices, not done much for several years and am still baffled by php. I was only getting to grips with php4 when 5 came out. As mewntiones I picked up PHP 5 and dynamic websites with php and dreamweaver by Larry Ullman back then and as now I found the coding he used didn't work on many of the examples despite the book being writteen for php5. I had to email him for an explanation (on more than a few occasions)
Now I have the New book PHP6 and MySQL5 for Dynamic Websites from which I take my other example that is failing on undefined indexes. I cannot for the life of me see why. No matter what method I use, get, post or request the values passed from the html variables are being rejected and the script is throwing indexing errors
example
HTML FORM value name="name"
php handler $name=$_REQUEST['name']
seems to me that the html is passing the value entered from user input in the name field to the server using post method, The php script then in turn is asking the server to pass on that value to the script via the $_REQUEST, or $_GET methods - explicitly i.e $_REQUEST['name']
So all should be Hunky Dory
except when you hit the submit button you are met with:
PHP Notice: Undefined index: name in C:\inetpub\wwwroot\php\handle_form.php on line 10
and so on for the other variables
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 1:44 pm
by redmonkey
With PHP it's not absolutely necessary to intialise a variable but it is good practise and failing to initialise the variable piror to using it in an expression will result in the PHP parser issuing a 'Undefined index' notice error (unless notice errors are supressed).
Initialising a variable is as simple declaring it with a value, that value can be a real world value, the value of another variable or the returned value of a function so, using your example....
.... is valid but if $_REQUEST['name'] doesn't exist then a notice error will be issued.
You could check what variables are available within the $_REQUEST array by using print_r i.e. .....
To go back to your 'if ($process)' problem, $process has not been declared prior to use so the if expression will always evaluate to false further more an undefined notice error will be issued.
From your code snippets I see that 'process' is a hidden variable with a hard coded value of '1' used within your form and your script is attempting to use that to decide if the form has been submitted or not. There are a couple of different ways to do it, it's really just a case of preference. As $process is only used once you could something like...
Code: Select all
if (isset($_REQUEST['process']) && $_REQUEST['process'] === '1')
.. by using isset() you will not get an undefined index notice error.
If you are going to be using the variable in multiple places you could first initialise the variable like this...
Code: Select all
$process = isset($_REQUEST['process']) ? $_REQUEST['process'] : false;
.... that uses the ternary operator but it's essentially an 'if else' type statement. Then similar to first example you could then use....
Code: Select all
if ($process === '1') // evaluates to true only if $process equals literal string '1'
if ($process) // evaluates to true if $process is any non zero value
I'd recommend you have a quick read of the following two links...
http://www.php.net/manual/en/language.v ... basics.php
http://www.php.net/manual/en/language.v ... ternal.php <-- this one deals with accessing variables from forms
Re: Hello - a question or two about PHP 5.3 code
Posted: Fri Jul 31, 2009 2:18 pm
by furby_fred
Thank you again redmonkey... I will investigate matters further a little later on as I have to pop out for a bit.
I think the other issue is quite possibly resolved. I have a spanish keyboard and it's pretty hard to find an english one here in Lima, Peru. Nightmare for programming and scripting as there is no backslash in evidence, you have to type ALT+64 to get @ and so on.... you can imagine what fun this is in command line as well as php.
Will need to look at the punctuation in the html form again as was suggested....
Will post back here later this evening having played with your suggestion
UPDATE
Hi again,
one of the problems I am now glad to say is fixed but the process one continues to give headaches. I got an amended code update from a guy on phpfreaks.com but now have an other issues.....to do with smtp configuration in my php.ini file.
I set it up to use my mac email account or another I have registered in Ireland and in each case tried using the outgoing smtp server id. I used the defaul port 25 or in the case of the mac account 993, neither one resolved the issue. Instead I got referred to spamhaus whio seemingly cannot find the server... I really am staryting to wonder if this is worth the hassle. I don't have an email account set up in outlook or outlook express for that matter - and don' intend to. Maybe I need to use the default mail port for this Vista machine and don't know what that is. I definitely do not want to set up any of my accounts on a Windows box - too much extra hassle in terms of spam and viruses etc. Mac does a fantastic job of keeping both to a bare minimum... at least apple mail or entourage with spamsieve on top! Spamsieve is by far the best antispam tool I have used to date and I have been through a few of them....
any ideas on how to get the mail port number. I am sure I know myself but the brain is tired having spemt nearly 2 days now trying to get a PHP/Apache/MySql set up on this box.... Not even sure if that wil fix my problem though!
Re: Hello - a question or two about PHP 5.3 code
Posted: Sat Aug 01, 2009 9:05 pm
by redmonkey
Port 993 is secure imap which is not what you need. The php.ini file specifies the outgoing mail server, if you don't have an outgoing mail server installed on your machine then you can normally just use the outgoing mail server details from your ISP. I've yet to come across an ISP that doesn't offer access to their outgoing mail server.