PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 1st, 2004, 10:07 PM
patience/thanks patience/thanks is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 patience/thanks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have newbie questions.

Good Day. I often times find that in reading on line documentation & php books, that I go off on tangents, as there is so much to learn, get lost and thereby never feel like I fully understand a concept. Wish I could afford training, but its not an option at this time. After carefully imbibing two beginner php books (carefully, mind you) and making my way through parts of Luke Wellington's book, various articles on php security issues, and the php.net site, I am at the point where I need to confirm that my understanding of some basic concepts are correct.

Here is a simple example of something I would like to do. I have a html form. I want to print out the user's input to a second screen for them to look at. Is there anything incorrect, insecure or outdated about this code? I can get it to work, but I've heard all the warnings about beginner php books AND I trashed a hard drive using IIS on my Windows PC and am just now recovering. I use a web hosting company now who does have register_globals turned off. I don't want to put it into a database yet.

The html form:

<form action="CALENDAR.PHP" method=post>
Daily Calendars<input type="text" name="dayqty" size=3 maxlength=3><br>
Monthly Calendar<input type="text" name="monthqty" size=3 maxlength=3><br>
<input type=submit value="Submit Order">
</form>

The Calendar.php page that will print out to the screen:

<?php
session_start();
$HTTP_SESSION_VARS['dayqty']=$dayqty;
$HTTP_SESSION_VARS['monthqty']=$monthqty;
$dayqty = $HTTP_POST_VARS['dayqty'];
$monthqty = $HTTP_POST_VARS['monthqty'];

///validation check

if (ereg("([^0-9])",$dayqty))
{
echo 'Please enter a numeric value in the <strong>daily calendar </strong>quantity box. <p><a href ="order.html">Back</a></p>';
exit;
}
if (ereg("([^0-9])",$monthqty))
{
echo 'Please enter a numeric value in the <strong>monthly calendar </strong>quantity box. <p><a href ="order.html">Back</a></p>';
exit;
}
if ($dayqty == '' && $monthqty == '')
{
echo 'You have not ordered anything! <a href ="order.html">Back</a></p>';
}
else
{
echo $dayqty.' Daily Calendars<br>';
echo $monthqty.' Monthly Calendars';
}
?>

Notes: I've heard that preg() is more secure.

Thanks for any input. Grasshopper is learning from his/her mistakes. I've been reading that it is a better practice to develop a template (using PEAR) than mix up your html and php, but I cannot find a simple example of this. Grasshopper hops to far ahead.

Reply With Quote
  #2  
Old September 2nd, 2004, 12:59 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
Looks good for a start. A few suggestions:

-use $_SESSION, $_POST and $_GET instead of the (older) $HTTP_ _VARS.
the $_ vars are globals, and less typing.

-check if a variable is set before assigning:
if(isset($_POST['dayqty']))
$dayqty = $_POST['dayqty'];
else
$dayqty = "";

-I'm also wondering about these lines:
$HTTP_SESSION_VARS['dayqty']=$dayqty; #etc
at this point in the program, $dayqty e.o. haven't been declared or assigned yet, making these statements useless.
Should they be moved to AFTER the POST fetches?

-in this case, the function is_numeric() might be easier, with the same effect, as you're only scanning for numbers, no special combinations.

-As for templates using PEAR, never heard of it. And I've been doing PHP for a while now. So if it's really something useful, they're keeping awfullly quiet about it.

Good luck.

Reply With Quote
  #3  
Old September 2nd, 2004, 06:25 PM
patience/thanks patience/thanks is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 patience/thanks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

Thanks for your reply - lets see if I here you & ask a couple of questions.
In regards to:

-check if a variable is set before assigning:
if(isset($_POST['dayqty']))
$dayqty = $_POST['dayqty'];
else
$dayqty = "";

Yes thanks for this reminder, I somehow missed it in the documentation. Does isset() mean that I am verifying or setting the variable to 'dayqty', rather than it being set automatically with a name by the user, which they can than run away with and do heavens knows what? I am foggy on this.

In regards to:

-I'm also wondering about these lines:
$HTTP_SESSION_VARS['dayqty']=$dayqty; #etc
at this point in the program, $dayqty e.o. haven't been declared or assigned yet, making these statements useless.

I've read documentation that says after I start the session, I need to check if the session variables are set (as you mentioned above) and than I need to register the session variables to access them (and I want to access it to print it out to the screen). It gives the examples:

$HTTP_SESSION_VARS['varName']=$varName; ///I'll assume this is form is likely to be depricated
///or the shorter
$SESSION['varName']=$varName;

Thanks for your input.

Reply With Quote
  #4  
Old September 3rd, 2004, 02:30 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
isset:
isset simply returns true if a variable has been set, that is, there is a space in memory reserved for it. It is an easy way to check if a page was called by the form it expects to be called by. See also the PHP manual entry for isset.

sessions:
what you say was true in earlier versions of php. You had to save your variables through session_register(), which took a variable from the scope it was called from, and stored it in the session. However, after they introduced the global $_SESSION array, you no longer need to do this, as you can freely write and read from this array (also see isset function). In fact, on a system with register_globals off, the old style session system might very well not even work, as it works with the normal variable names, and not an associative array.
As for declaring them, I think what they meant was on the page where you INITIALIZE the session variable, you say:
PHP Code:
 $_SESSION['varname'] = $varname

after which you can read it on the next page like this:
PHP Code:
if(isset($_SESSION['varname']))
    
$varname $_SESSION['varname'];
else
    
$varname ""


and remember it's $_SESSION, not $SESSION. :-)

Reply With Quote
  #5  
Old September 3rd, 2004, 06:45 PM
patience/thanks patience/thanks is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 patience/thanks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Great, thanks for passing on the info.

Next I'm going to be checking some code I wrote for inputting the content into a mysql database using phpadmin. Only a php nut would do this on a weekend!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > I have newbie questions.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway