General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

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:
  #1  
Old November 25th, 2002, 12:19 PM
johnn johnn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Southern California, USA
Posts: 48 johnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
$_post

Hi,
Which way do you prefer, redefined $abc= $_POST['abc']; and use $abc all over script or just use $_POST['abc'] all over the script?

Thanks,
John

Last edited by johnn : November 25th, 2002 at 12:55 PM.

Reply With Quote
  #2  
Old November 25th, 2002, 12:38 PM
Taelo Taelo is offline
5B's
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: PC, FL
Posts: 366 Taelo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 59 sec
Reputation Power: 16
hmm good question,...never really thought about it before,...
I can say that for now,...I am using $_POST['var'] within my scripts.

I am curious if it there is less strain on the interpreter to do

$_POST['looooooong_var_name'] = $var;

in other words,...replacing a long variable with a shorter one.
__________________
-- Jason

Reply With Quote
  #3  
Old November 25th, 2002, 01:19 PM
fatal fatal is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 9 fatal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
$var = $_POST all the way, only because it makes code look more cleaner and easier to look at. Rater than having $_POST[] all over the place, i grab all the $_POST varibles at the top of the script.

also,
$_POST['var2'] = $var3; - wouldnt work, but

PHP Code:
<?php
   $var4 
$_POST['var2'];
   echo 
$var4;
?>


will work.

Reply With Quote
  #4  
Old November 25th, 2002, 02:32 PM
MastaM00 MastaM00 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 6 MastaM00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Disagree with the last post 100%.

Why redefine something you already have defined just so it looks "PRETTIER"?

There's a reason why REGISTER GLOBALS is turned off be default in the later versions of PHP. Why defeat the purpose by doing this?

Reply With Quote
  #5  
Old November 25th, 2002, 03:13 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Actually none of you are 100% right.

First on the point of 'prettiness', many people would define this as 'neatness' and, furthermore, 'readability'. I find it easier to manage my variables when they're in container arrays, such as $_POST, $_GET, it doesn't really matter in the long run. Obviously, using $_POST['var'] takes more typing than just $someVar, but it's easier to identify and recognize.

That brings me to my second point. Something none of you touched on was the fact that you HAVE to verify all your REQUEST data in order to keep your application safe and running smoothly. If you're letting someone upload a file, check the size (NOT by relying on HTML). If they're putting in text, addslashes if you need to, check for html if it's not allowed, etc. You have to look at REQUEST data as unreliable and unsafe.

Finally, a basic example...
PHP Code:
if ($_POST['submit']) {
   if (
$_POST['strUserText']) {
$strUserText=addslashes(htmlspecialchars($_POST['strUserText']));
      }
    else {
      echo 
'You did not submit any text!';
    }



This is obviously pretty basic, the user is submitting the form clicking on a button named submit. The form method is post. The user is submitting the field strUserText. We remove any html special characters from the text, and add slashes to escape out any data that may cause php (or our database) any trouble.

Once that's done, $strUserText is ready to go into your db, or file, or whatever. I could've just as easily done $_POST['strUserText'], but it's just simpler to type strUserText. Additionally, preserving the original user submitted values can come in handy for later processing.

In the end, it's a matter of preference. What's most important is that you are CONSISTENT.

Reply With Quote
  #6  
Old November 25th, 2002, 08:33 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
ok, just my 2c

firstly while it maybe slightly easier to use a variable like $value instead of $_POST['value']

there are several advantages to using $_POST, etc

firstly when you have

$value = $_POST['value']

the server then has to store another variable with the same data. This is just a waste of resources. This wastes time and resouces of the server.

Its easier to find a variable with $_POST then $variable.

having said that.

I do sometimes define one of my _POST arrays as a normal variable, if im using that variable in alot of strings.

eg

"this is a $test" x50

instead of

"this is a {$_POST['test']}"

using $test over {$_POST['test']} in a string does have a slight speed advantage.

So if i have to use a post var in more then 3 strings i usally convert it to a normal var

Reply With Quote
  #7  
Old November 25th, 2002, 08:58 PM
Taelo Taelo is offline
5B's
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: PC, FL
Posts: 366 Taelo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 59 sec
Reputation Power: 16
Ben,.....what if you do in fact have $_POST['var'] x 50,..lol,...is it more of a server strain to reference it by the $_POST array then by a renamed $var?

Reply With Quote
  #8  
Old November 25th, 2002, 09:39 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
There is only more strain on the server if the $_POST is located in a string

if you using it like

echo $_POST['var'];

or

$sum = $_POST['var'] * 50;

then there is no point in defining a new variable for it.

Reply With Quote
  #9  
Old November 26th, 2002, 01:22 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
If you want to simplify your code by eliminating $_POST just use a reference to the post value you want. If you're going to do a lot of processing on it, it's probably just easier to put it another var. but if you just want to not have to type post, and not have memory problems, just do
$var =& $_POST['var'];
that doesn't really waste resources, since a reference only contains a memory location ;P

Reply With Quote
  #10  
Old November 26th, 2002, 01:56 PM
MastaM00 MastaM00 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 6 MastaM00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Doing references or something like that is fine for SMALL projects of 1 or MAYBE 2 people.

If you did something like that in a large project I would personally seek you out and make your life miserable. You're introducing greater and greater possibilities for variable collision like this.

Have you ever had post/cookie/file variables colliding? THAT'S WHY YOU LEAVE THE VARIABLES IN THEIR RESPECTIVE CONTAINERS. You're eliminating the possibility of variable collision. And by creating these extra variables you're introducing the possibility of variable poisoning AGAIN.

Reply With Quote
  #11  
Old November 26th, 2002, 03:14 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
You shouldn't have variables colliding anyway, not request data... but I won't get into an argument about good coding practices.

As I've said, and can be quoted on over and over, USE THE SUPERGLOBALS. I simply gave a legitimate alternative if someone doesn't want to type out $_POST 75,000 times to process a website registration form or something =( I don't think most people on this forum are making large scale ecommerce sites, so, while I appreciate your input, the hostile tone was a bit unneccessary.

Reply With Quote
  #12  
Old November 26th, 2002, 04:17 PM
Taelo Taelo is offline
5B's
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: PC, FL
Posts: 366 Taelo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 59 sec
Reputation Power: 16
/me changes name to =(

/me no like hostility :/

Reply With Quote
  #13  
Old November 26th, 2002, 05:04 PM
MastaM00 MastaM00 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 6 MastaM00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by crazytrain81
You shouldn't have variables colliding anyway, not request data... but I won't get into an argument about good coding practices.

As I've said, and can be quoted on over and over, USE THE SUPERGLOBALS. I simply gave a legitimate alternative if someone doesn't want to type out $_POST 75,000 times to process a website registration form or something =( I don't think most people on this forum are making large scale ecommerce sites, so, while I appreciate your input, the hostile tone was a bit unneccessary.


It was meant in a sarcastic manner but yeah that doesn't carry to well on the net. sorry about that.

Reply With Quote
  #14  
Old November 26th, 2002, 05:10 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
It doesnt matter which method you use, as long as it parses the correct html code to the user.

Reply With Quote
  #15  
Old November 26th, 2002, 05:36 PM
Lindset Lindset is offline
weirdomoderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Alta, Norway
Posts: 370 Lindset User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to Lindset Send a message via AIM to Lindset
In my opinion the superglobals should always contain their original value. If you need to change the data, make a new working variable. I feel the same way with functions/methods.

This way, if you have to access the original data somewhere else in the code, you won't have to do any changes to the code in order to get that data. Let's take a look at an example:

PHP Code:
function setUserName($name) {
    
$name addslashes($name);

    if (
is_string($name)) {
        ...
insert into database....
        
$this->_userName $name;
    }



oops, we're assigning the variable with slashes added to _userName.. I agree, this is a bad example, but bear with me. Instead of doing it like that, you can assign $name to a new variable in the beginning of the method.. You could make your own prefixes for variables.. Let's say, i for input and w for working.

PHP Code:
function setUserName($iName) {
    
$wName $iName;
    
$wName addslashes($wName);

    if (
is_string($wName)) {
        ...
insert into database....
        
$this->_userName $iName;
    }



As I said before, this is a rather bad example, but hopefully you'll see what I mean..

So instead of using and changing the data in the superglobals all over the script, at least I recommend making a local copy that you can work with, and change the value of as much as you'd like, while still being able to access the original data through the superglobal.

Well, this is at least how I feel about the issue...
__________________
Best Regards,
Håvard Lindset

Reply With Quote
  #16  
Old January 5th, 2004, 10:55 PM
swiftouch swiftouch is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Utah
Posts: 1 swiftouch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to swiftouch
I agree

I agree with the guy that said just use $_POST['var']

I have so many variables in some projects, if I start "renaming" them, I am kickin' myself in the arse later.

Reply With Quote
  #17  
Old January 6th, 2004, 07:20 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to dhouston
I inherited a bunch of code wherein the programmer wasn't using superglobals at all to start with (for $_GET["var"] or $_POST["var"] or $_SESSION["var"], he was always just using $var). On top of that, he was creating all sorts of other variable names, many of which were nondescriptive and hard on the eyes. Reading the code and figuring out where things are coming from has proven to be a real nightmare. I'm a strong advocate of using the superglobals.

As for saving old user input, why save it if you don't need it? If you find out you need to keep an old piece of input, duplicate the one piece and save that much in the way of resources. Or if you must keep it all for some reason (again, since this is run-time and not stored data, why bother if you're not going to use it?), I'd consider copying the superglobal into an array for safe keeping and still using the superglobal throughout the script to keep scope transparent.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > $_post


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap