php-general Digest 11 Sep 2006 10:55:37 -0000 Issue 4341

Topics (messages 241566 through 241576):

Re: Newbie question about <?= ?>
        241566 by: Satyam

PHP 4 OOP, re-using a object?
        241567 by: Micky Hulse
        241568 by: M. Sokolewicz
        241569 by: Micky Hulse
        241570 by: Christopher Weldon
        241571 by: Micky Hulse

Ajax and PHP: XMLHTTP
        241572 by: Micky Hulse

Re: Date maths
        241573 by: Paul Scott

Re: FUNCTION TO CHECK IMAGE
        241574 by: BBC
        241575 by: BBC

does magic_quotes_gpc  prevents sql injection through forms?
        241576 by: Reinhart Viane

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---

----- Original Message ----- From: "Jon Anderson" <[EMAIL PROTECTED]>
To: <php-general@lists.php.net>
Cc: "Al" <[EMAIL PROTECTED]>
Sent: Sunday, September 10, 2006 9:16 PM
Subject: Re: [PHP] Re: Newbie question about <?= ?>


Al wrote:
Structurally, there is a far better way to compile your html pages. This approach is easier to design and debug and it is faster since it sends one complete packet instead of one for every short tag. And, it saves using ob_start() and ob_flush().

Consider:

$report= '';

$report .= function() [or whatever]

..... repeat as necessary to assemble your complete page.

Then simply

echo $report;
I thought I'd look into this, because I'm a bit of a performance nut - I like my code to run as fast as possible at all times. I wrote up a quick buffer v.s. direct "benchmark" for this, and the winner is clear: direct output is much faster. (If my example below isn't what you meant, please let me know. I'm always happy to hear new ways to improve my code.)

Best of 3 runs with apache bench (concurrency 10, 1000 requests total):
Direct output: 582 requests a second
Buffer var: 286 requests a second

I believe the margin would get wider with real-world usage, as the buffer variable would increase in size. My test code is copied below.

jon

--- "Direct output": testecho.php ---

<html>
<head>
<style type="text/wastespacetosimulateastylesheet">
style1 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style2 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style3 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style4 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
</style>
</head>
<body><table>

<?php for ($x=0;$x<1000;$x++) { ?>
   <tr><td>X is <?= $x ?></td></tr>
<?php } ?>

</table></body>
</html>

--- "Buffered output": testbuffer.php ---

<?php

$buffer = '
<html>
<head>
<style type="text/wastespacetosimulateastylesheet">
style1 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style2 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style3 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
style4 {
   a = 1;
   b = 2;
   c = 3;
   d = 4;
   e = 5;
   f = 6;
}
</style>
</head>
<body><table>';

for ($x=0;$x<1000;$x++) {
   $buffer .= "<tr><td>X is $x</td></tr>";
}

$buffer .= '</table></body>
</html>';

echo $buffer;
?>

--
In my message I was careful to mention that stepping in and out of PHP was good 'for the longer invariable parts of immutable HTML'. What could be considered 'longer' is certainly a matter discussion, your results prove that this is not long enough. Notice that when the parser finds the '<?=', it converts it into the equivalent of "<? echo", thus, though the echo is not explicitly there, from the parser on is as if it were. Then, since you have an echo, why not use it for all of the output? The equivalent to what I showed as the second best, which would be the first best with 'shorter' strings would be the following:

for ($x=0;$x<1000;$x++) {
   echo ' <tr><td>X is ' , $x , '</td></tr>';
}

Can you try and time that one so we have comparable results? This one should be second best:

for ($x=0;$x<1000;$x++) {
   echo "<tr><td>X is $x</td></tr>";
}

Back again to what would be 'longer', well, in your example, the whole header, up to the loop itself should be faster if sent out of PHP. Likewise, you could echo $buffer right after the loop, drop out of PHP and send the footer as plain HTML. This, of course, is harder to time since it happens only once. I admit though that I did time the options I listed and on the 'dropping in and out of PHP' I'm relying on the PHP manual ( see http://www.php.net/manual/en/language.basic-syntax.php, the first paragraph after the examples) and the source of the lexical scanner, which supports that, though your numbers do contradict it. Interesting.

Satyam

--- End Message ---
--- Begin Message ---
Hi,

I am slowly learning how to use classes properly... quick question:

# Create the object:
$doSplash = new RandomSplash(); // Create the object.

Shouldn't I be able to call that object like below, throughout my page:

...HTML...
<?php $doSplash->randomize('css'); ?>
...HTML...
<?php $doSplash->randomize('xhtml top'); ?>
...HTML...
<?php $doSplash->randomize('xhtml bot'); ?>
...HTML...

Sorry, this is probably a noob question... I am pretty good at using functions, but classes are still new to me.

Any help, even a RTFM link, would be really appreciated. :)

Have a great day!
Cheers,
Micky

--
 Wishlist: <http://snipurl.com/vrs9>
   Switch: <http://browsehappy.com/>
     BCC?: <http://snipurl.com/w6f8>
       My: <http://del.icio.us/mhulse>    

--- End Message ---
--- Begin Message ---
Micky Hulse wrote:
Hi,

I am slowly learning how to use classes properly... quick question:

# Create the object:
$doSplash = new RandomSplash(); // Create the object.

Shouldn't I be able to call that object like below, throughout my page:

...HTML...
<?php $doSplash->randomize('css'); ?>
...HTML...
<?php $doSplash->randomize('xhtml top'); ?>
...HTML...
<?php $doSplash->randomize('xhtml bot'); ?>
...HTML...

Sorry, this is probably a noob question... I am pretty good at using functions, but classes are still new to me.

Any help, even a RTFM link, would be really appreciated. :)

Have a great day!
Cheers,
Micky

you should be able to, and it works fine for me. www.php.net/oop will help, if you haven't read it.

- tul

--- End Message ---
--- Begin Message ---
Micky Hulse wrote:
Any help, even a RTFM link, would be really appreciated. :)

Hi all,

A fellow list member gave answered my question off-list and pointed me here:

http://www.php.net/oop

Going to (re)read... It has been a while since I last worked with classes. :)

I asked because I was getting an error with my code... I thought it was how I was calling it.

Anyway, have a great day,
Cheers,
Micky


--
 Wishlist: <http://snipurl.com/vrs9>
   Switch: <http://browsehappy.com/>
     BCC?: <http://snipurl.com/w6f8>
       My: <http://del.icio.us/mhulse>    

--- End Message ---
--- Begin Message ---
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Micky Hulse wrote:
> Micky Hulse wrote:
>> Any help, even a RTFM link, would be really appreciated. :)
> 
> Hi all,
> 
> A fellow list member gave answered my question off-list and pointed me
> here:
> 
> http://www.php.net/oop
> 
> Going to (re)read... It has been a while since I last worked with
> classes. :)
> 
> I asked because I was getting an error with my code... I thought it was
> how I was calling it.
> 
> Anyway, have a great day,
> Cheers,
> Micky
> 
> 

What's the error, and how do you have the class and functions defined?

- --
Christopher Weldon, ZCE
President & CEO
Cerberus Interactive, Inc.
[EMAIL PROTECTED]
979.739.5874
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFBMjMZxvk7JEXkbERArjTAKCVd57A8Pj+aoaY0iF4BU/5A1dmawCeP3ku
h4bOmUAeBphVXvOfnnpOwUQ=
=JPWQ
-----END PGP SIGNATURE-----

--- End Message ---
--- Begin Message ---
Christopher Weldon wrote:
What's the error, and how do you have the class and functions defined?

Hi Christopher, thanks for asking.  :)

Actually, it was one of those silly, right in front of my face, logic errors...

class RandomSplash {
var $alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm');
function randomize($what) {
        switch($what) {
                case 'css':
                $data = $this->$alphabet;
                ...
                ...
                ...
}
}

See the error?

$data = $this->$alphabet;

should've been:

$data = $this->alphabet;

At first I thought it was how I was calling the class... all works now though. :)

Thanks for asking. Let me know if you see a better way to do the above though, I am pretty new to using classes, but I am pretty handy when it comes to functions. :D

Thanks,
Cheers,
Micky

--
 Wishlist: <http://snipurl.com/vrs9>
   Switch: <http://browsehappy.com/>
     BCC?: <http://snipurl.com/w6f8>
       My: <http://del.icio.us/mhulse>    

--- End Message ---
--- Begin Message ---
<?=$_SERVER['PHP_SELF']?>

Can I replace the above with some sort of XMLHTTP request?

Googling now... thought I would ask here first.

Any good links to tuts that might cover that sort of thing?

Kinda thinking about plugging some Ajax into a random image php script.

TIA. :)
Cheers,
Micky

--
 Wishlist: <http://snipurl.com/vrs9>
   Switch: <http://browsehappy.com/>
     BCC?: <http://snipurl.com/w6f8>
       My: <http://del.icio.us/mhulse>    

--- End Message ---
--- Begin Message ---
Dave, 

Luckily for you there is a whole whack of functions made specifically
for these issues...

On Sun, 2006-09-10 at 21:19 +0100, Dave Goodchild wrote:
> So, when an event-holder enters a repeating event, the main event details
> are held in the events table, and all the relevant dates for the event (they
> specify the first date of the event and the last date it is to be held) are
> entered into the calendar table by increment (ie timestamp intervals by 24
> hours, 7 days or monthly, and I know monthly is going to be tricky as it is
> a fluctuating interval).
> 

Have a look at the PHP manual entries for date() and strtotime(). There
are a few others, but this should get you well started!

There is also a PEAR::Date object that you may find useful, as well as a
calendar object IIRC. 

There are a few basics that you want to keep in mind though:

1. strtotime works well, but make sure that your date/time strings
conform to the GNU datetime specs.

2. There are 86400 seconds in a day.

3. Date also accepts parameters like +1month and -1month, this works for
days, weeks, months etc. 

4. Google "php calendar class and php date class" you will get like a
bazillion resources that will really help out.

HTH a bit...

--Paul



All Email originating from UWC is covered by disclaimer 
http://www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm 

--- End Message ---
--- Begin Message ---
> BBC wrote:
> > Hi all.
> > I'm having a problem regarding image handling. Sometimes an image can be 
> > showed in browser normally (in my computer) but when I
> > upload it into my site that image is not valid, so there is an issue in my 
> > getimagesizes() function.
> > Is there any function to recover such problem? I used 
> > "if(file_exists($image_path))" but it couldn't solve the problem.
> > Input would be appreciated
>
> - From what it sounds like you are doing, you are uploading a file through
> some form, then manipulating it's size. Is this correct?

Yes it is, but the uploading was going well (size is ok). When I show the image 
on the web using getimagesizes(), 'sometimes' such
image size couldn't be resolved.
Is there any function to define that image is ok or not?

> What happens when you stop manipulating it's size and try to view just the 
> uploaded
> file?

Size of image (after or before uploading) would be same. but the issue still 
there (sometimes)

--- End Message ---
--- Begin Message ---
> At 12:49 AM -0700 9/10/06, BBC wrote:
> >Hi all.
> >I'm having a problem regarding image handling. Sometimes an image 
> >can be showed in browser normally (in my computer) but when I
> >upload it into my site that image is not valid, so there is an issue 
> >in my getimagesizes() function.
> >Is there any function to recover such problem? I used 
> >"if(file_exists($image_path))" but it couldn't solve the problem.
> >Input would be appreciated
> >
> 
> How are you uploading your image?
I upload a file through some form then I manipulate it's size

> 
> What is reporting your image is not valid?
> 
Size of image can't be define seem's like the image is not there, but it's not  
happened all the time.

--- End Message ---
--- Begin Message ---
After some comments on my code I went on a 'fieldtrip' to learn about sql
injection...

Now after testing some examples with single and double quotes and mysql
comment (--) I haven't find any way to insert edit or delete any data in the
database.
The loginscript is rather simple:

$query="SELECT FROM persons WHERE login='$login' AND password='$password'";
$result=mysql_query($query) or die(mysql_error());

The form has action POST.
Now magic_quotes_gpc escapes every quote I insert.

Does this mean with magic_quotes_gpc on I am secured enough concerning
mysql-injection through forms?

Thx

--- End Message ---

Reply via email to