php-general Digest 20 Nov 2008 13:37:45 -0000 Issue 5800

Topics (messages 283579 through 283592):

Re: while-question
        283579 by: Shawn McKenzie

fread() behaviour
        283580 by: Rene Fournier
        283582 by: Craige Leeder
        283586 by: Stut

Re: Invalid Arguements
        283581 by: Jim Lucas
        283585 by: Lars Torben Wilson

in_array breaks down for 0 as value
        283583 by: Yashesh Bhatia
        283584 by: Lars Torben Wilson
        283587 by: Stut

Re: PHP Warning: HTTP request failed -- BSD resource limit reached?
        283588 by: Nathan Rixham

Re: anchor name on URL
        283589 by: Dan

store class zithin session
        283590 by: Alain Roger
        283591 by: Stut
        283592 by: Yeti

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:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
bruce wrote:
> interesting points regarding college and programming..
> 
> my degrees bsee/msee covered alot more than pure programing.. as a double 
> ee/cs, the ability to articulate an issue/problem, and bring to mind a cogent 
> thought process was valuable. the ability to understand how different 
> algorithms worked, and how code actually played with the lower intracacies of 
> the processor where quite valuable. 
> 
> and no.. i'm no longer the engineer i was a time ago..
> 
> so.. interesting...
> 

They must have been case-insensitive languages, unlike English.  :-)

-Shawn

--- End Message ---
--- Begin Message --- I'm trying to understand something about fread(). I'm using fread() on an incoming socket stream that will send, for example, 26630 characters:

while ( ($buf=fread($read[$i], 8192)) != '' ) {
        $sock_data .= $buf;
        usleep(1000);
        echo ".";
        }
echo ",";


As soon as the socket client sends the data, immediately the server will echo:

................................................................................................................................................

Then wait nearly a minute, and echo:

,

So my question is, why does fread wait if there is nothing more to read? Shouldn't it return immediately? (That's what I want.) And as for the delay, it's there so that if the incoming data is a little slow, it has time to catch up with fread. Thanks.

...Rene

--- End Message ---
--- Begin Message ---
Rene Fournier wrote:
So my question is, why does fread wait if there is nothing more to read? Shouldn't it return immediately? (That's what I want.) And as for the delay, it's there so that if the incoming data is a little slow, it has time to catch up with fread. Thanks.

...Rene

I do believe:

No, because fread reads to EOF, which is not present. Just because there is no text being sent over the steam, does not mean EOF is present.

I could be wrong, but that's the general idea if I recall correctly.

- Craige

--- End Message ---
--- Begin Message ---
On 20 Nov 2008, at 01:29, Rene Fournier wrote:
I'm trying to understand something about fread(). I'm using fread() on an incoming socket stream that will send, for example, 26630 characters:

while ( ($buf=fread($read[$i], 8192)) != '' ) {
        $sock_data .= $buf;
        usleep(1000);
        echo ".";
        }
echo ",";


As soon as the socket client sends the data, immediately the server will echo:

................................................................................................................................................

Then wait nearly a minute, and echo:

,

So my question is, why does fread wait if there is nothing more to read? Shouldn't it return immediately? (That's what I want.) And as for the delay, it's there so that if the incoming data is a little slow, it has time to catch up with fread. Thanks.

As Craige already mentioned, fread will read bytes until it meets an EOF so unless the other side sends one or closes the socket fread will wait for the number of characters you've asked for (8192) or a timeout (which is the delay you're seeing). In other words it's not detecting the end of the data, it's just timing out waiting for more.

You ideally want to have the other side tell you how many bytes it's going to send. If you can't do that then hopefully the data you're receiving has some sort of structure so you can check the incoming data for some terminating string. If not then you've got a problem that can't be reliably solved. You could mess around with the timeout and/or make use of functions like socket_select to check for waiting data, but what you'll end up with will be problematic on slow connections and generally unreliable.

-Stut

--
http://stut.net/

--- End Message ---
--- Begin Message ---
Terion Miller wrote:
Actually it did at one point have bannersize[#] # being the numbers
1-however many were there
I've since gotten rid of that and made it a select.
and gotten rid of the implode all together because it wouldn't work in
either case and the more I read the more confused I got.
Terion


Why don't you show us a snippet of code that is the form page for this.

Let us see what you are trying to describe to us.

Even if you switched it to a <SELECT ...></SELECT> the name attribute still needs to contain the brackets if you expect to pass more then one <SELECT> field in the same form.

--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare


--- End Message ---
--- Begin Message ---
2008/11/19 Robert Cummings <[EMAIL PROTECTED]>:
> On Wed, 2008-11-19 at 19:49 +0000, Ashley Sheridan wrote:
>> On Wed, 2008-11-19 at 08:31 -0600, Terion Miller wrote:
>> > I am still getting the Invalid arguement error on this implode:
>> >
>> > if (isset($_POST['BannerSize'])){$BannerSize =
>> > implode(',',$_POST['BannerSize']);} else {$BannerSize = "";}
>> >
>> > I have moved the ',', from the beginning to the end of the statement and
>> > nothing works is there any other way to do this, basically there is a form
>> > and the people entering work orders can select different sized banners they
>> > need, which goes into the db as text ....so...argh...
>>
>> As mentioned quite a few times before, you cannot change the order of
>> the arguments, it will not work! There is no indicator in the PHP Manual
>> page for this function either that says you can switch the orders, so
>> I'm not sure why you think you can.
>
> In PHP 5.2.6 and in PHP 4.4.9 I get the same output for the following:
>
> <?php
>
>    $foo = array( 1, 2 );
>
>   echo implode( $foo, ',' )."\n";
>   echo implode( ',', $foo )."\n";
>
> ?>
>
> I believe, that a long time ago it was added as a feature so that PHP
> would detect inverted parameter order on this particular function and
> output appropriately.
>
> It is probably undocumented so as to encourage a single documented
> order.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP

It was added a long time ago, yes, and it is indeed documented (as
already noted in this thread). I'm not sure which copy of the manual
Ash is reading from, but the one I'm looking at documents this
behaviour. And it's right there in the PHP source code (and easily
testable, as you have shown).


Torben

--- End Message ---
--- Begin Message ---
Hi.

  I wanted to use in_array to verify the results of a form submission
for a checkbox and found an interesting
behaviour.

$ php -v
PHP 5.2.5 (cli) (built: Jan 12 2008 14:54:37)
$

$ cat in_array2.php
<?php
$node_review_types = array(
                           'page'       => 'page',
                           'story'      => 'story',
                           'nodereview' => 'abc',
                           );

if (in_array('page', $node_review_types)) {
  print "page found in node_review_types\n";
 }
if (in_array('nodereview', $node_review_types)) {
  print "nodereview found in node_review_types\n";
 }

?>
$ php in_array2.php
page found in node_review_types
$

This  works fine. but if i change the value of the key 'nodereview' to
0 it breaks down.

$ diff in_array2.php in_array3.php
6c6
<                            'nodereview' => 'abc',
---
>                            'nodereview' => 0,
$

$ php in_array3.php
page found in node_review_types
nodereview found in node_review_types
$

Any reason why in_array is returning TRUE when one has a 0 value on the array ?

Thanks.

Yashesh

--- End Message ---
--- Begin Message ---
2008/11/19 Yashesh Bhatia <[EMAIL PROTECTED]>:
> Hi.
>
>  I wanted to use in_array to verify the results of a form submission
> for a checkbox and found an interesting
> behaviour.
>
> $ php -v
> PHP 5.2.5 (cli) (built: Jan 12 2008 14:54:37)
> $
>
> $ cat in_array2.php
> <?php
> $node_review_types = array(
>                           'page'       => 'page',
>                           'story'      => 'story',
>                           'nodereview' => 'abc',
>                           );
>
> if (in_array('page', $node_review_types)) {
>  print "page found in node_review_types\n";
>  }
> if (in_array('nodereview', $node_review_types)) {
>  print "nodereview found in node_review_types\n";
>  }
>
> ?>
> $ php in_array2.php
> page found in node_review_types
> $
>
> This  works fine. but if i change the value of the key 'nodereview' to
> 0 it breaks down.
>
> $ diff in_array2.php in_array3.php
> 6c6
> <                            'nodereview' => 'abc',
> ---
>>                            'nodereview' => 0,
> $
>
> $ php in_array3.php
> page found in node_review_types
> nodereview found in node_review_types
> $
>
> Any reason why in_array is returning TRUE when one has a 0 value on the array 
> ?
>
> Thanks.

Hi Yasheed,

It looks like you've found the reason for the existence of the
optional third argument to in_array(): 'strict'. In your second
example (in_array3.php), what happens is that the value of
$node_review_types['nodereview'] is 0 (an integer), so it is compared
against the integer value of the first argument to in_array(), which
is also 0 (in PHP, the integer value of a string with no leading
numerals is 0). In other words, in_array() first looks at the first
element of $node_review_types and finds that it is a string, so it
compares that value as a string against the string value of its first
argument ('nodereview'). Same goes for the second element of
$node_review_types. However, when it comes time to check the third
element, in_array() sees that it is an integer (0) and thus compares
it against the integer value of 'nodereview' (also 0), and returns
true.

Make any sense? The problem goes away if you give a true value as the
third argument to in_array(): this tells it to check the elements of
the given array for type as well as value--i.e., it tells in_array()
to not automatically cast the value being searched for to the type of
the array element being checked.


Torben

--- End Message ---
--- Begin Message ---
On 20 Nov 2008, at 06:55, Yashesh Bhatia wrote:
 I wanted to use in_array to verify the results of a form submission
for a checkbox and found an interesting
behaviour.

$ php -v
PHP 5.2.5 (cli) (built: Jan 12 2008 14:54:37)
$

$ cat in_array2.php
<?php
$node_review_types = array(
                          'page'       => 'page',
                          'story'      => 'story',
                          'nodereview' => 'abc',
                          );

if (in_array('page', $node_review_types)) {
 print "page found in node_review_types\n";
}
if (in_array('nodereview', $node_review_types)) {
 print "nodereview found in node_review_types\n";
}

?>
$ php in_array2.php
page found in node_review_types
$

This  works fine. but if i change the value of the key 'nodereview' to
0 it breaks down.

$ diff in_array2.php in_array3.php
6c6
<                            'nodereview' => 'abc',
---
                          'nodereview' => 0,
$

$ php in_array3.php
page found in node_review_types
nodereview found in node_review_types
$

Any reason why in_array is returning TRUE when one has a 0 value on the array ?

That's weird, 5.2.6 does the same thing. There's actually a comment about this on the in_array manual page from james dot ellis at gmail dot com...

<quote>

Be aware of oddities when dealing with 0 (zero) values in an array...

This script:
<?php
$array = array('testing',0,'name');
var_dump($array);
//this will return true
var_dump(in_array('foo', $array));
//this will return false
var_dump(in_array('foo', $array, TRUE));
?>

It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array returns TRUE. Use strict mode to work around this peculiarity. This only seems to occur when there is an integer 0 in the array. A string '0' will return FALSE for the first test above (at least in 5.2.6).

</quote>

So use strict mode and this problem will go away. Oh, and please read the manual before asking a question in future.

-Stut

--
http://stut.net/

--- End Message ---
--- Begin Message ---
Rene Fournier wrote:
On 19-Nov-08, at 12:52 PM, Nathan Rixham wrote:

Rene Fournier wrote:
Hi,
I have four identical command-line PHP scripts running, and each will frequently fetch some data from another server via file_get_contents(). By frequently, I mean on average, every second. Periodically, one of the processes (command-line PHP scripts), will fail on file_get_contents(), with the error message:

first thing that springs to mind is some form of hardware limitation, quite sure it's not php - could be a firewall with flood protection (or even your own isp's anti malware set-up) to combat it try binding the outgoing request to a random ip each time (if you have multiple ip's on the box) [context: socket -> bindto]

That could explain it, except that all the traffic is on the same LAN. There's no firewall between Server A and Servers B and C.

next up (very unlikely) but possibly outgoing port conflict where the previous local port is still closing whilst trying to be re-opened.

That's interesting. I will look into that.

to get an ideal fix though you'll want to move away from file_get_contents() as you're not doing things

Yes, I've also read that CURL is preferred to file_get_contents for reasons of performance and security. I'm going to try that too.


the most efficient way; HTTP/1.1 allows you to keep a port open and make multiple requests through the same socket/connection, simply keep the socket open and don't send a connection: close header after the request. (i say simply but you'll be needing to make you're own, or find a good, http handler that allows you to write raw requests and decode the raw http responses that come back)

best of luck; feel free to post your code incase anything jumps out as obvious.



I will let you know how it goes. Thanks for the advice!

...Rene


had another thought, it could be the web server you're requesting that is locking up, not enough worker threads, running cpu high etc etc - worth checking
--- End Message ---
--- Begin Message ---
>
> The Webkist engine afaik is licensed under the GPL, because of the use
> of the code from the original KHTML. I'm not sure how this fits with M$
> proprietary plan however...
>

Webkit is licensed under LGPL and BSD licenses.

> You say that, have you heard the latest for IE9? They're already
> planning it, and apparently it's going to use the Webkit engine!

Actually they haven't planned that at all. There was a comment by Steve
Ballmer in an interview that it was an interesting project, and something
they may look at, but that comment was quickly countered the next day by
Microsoft PR. Also, 'look at' != 'adopt'. They have no intention of using
Webkit, as they feel it important that they're able to continue to 'extend
the web' with proprietary additions to Trident. ;)

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

i have a class and i would like to store it zithin session.
i was thinking to use serialize/unserialize but it does not work.

any idea how to do it ?
thx.

F.

--- End Message ---
--- Begin Message ---
On 20 Nov 2008, at 11:01, Alain Roger wrote:
i have a class and i would like to store it zithin session.
i was thinking to use serialize/unserialize but it does not work.

any idea how to do it ?

Alain, you've been on this list long enough to know that "it does not work" is not enough information for people to be able to help you.

Firstly there is no need to serialise anything going into the session. Secondly I assume you want to store objects not classes. Thirdly there is no reason why it should not work, but there are some caveats you need to be aware of...

* The class definition must have been loaded before session_start() is called, otherwise you'll end up with an object of type stdClass rather than your class.

* Resources stored inside the object must be cleaned up in __sleep() and can be recreated in __wake() as they will not be stored in the session along with the rest of the object.

If you still can't get it to work after reading this, send us the code.

-Stut

--
http://stut.net/

--- End Message ---
--- Begin Message ---
If you can't load the class before calling session_start you can store
the serialized object in a file and simple set a
$_SESSION['path_to_file'] session variable..

EXAMPLE:
<?php
session_start();

//some code

class apple_tree {
var $apples = 17;
}

$temporary_file = 'appletree.txt';
$file_content = serialize(new apple_tree());

if ($fp = fopen($temporary_file, 'w')) {
fwrite($fp, $file_content);
$_SESSION['path_to_file'] = $temporary_file;
fclose($fp);
}

?>

--- End Message ---

Reply via email to