Re: [PHP] process creation

2009-01-23 Thread Török Alpár
2009/1/23 bruce bedoug...@earthlink.net

 A simple question (or so I thought).

 Does php allow an app to create/start a process/application that can
 continue to run on its own, after the initiating program/app terminates?

 It appears that the spawning/forking functions might work, but the child
 apps would be in a zombie status, and couldn't be killed by an external
 program.

 Basically, I'd like to create a bunch of test apps/processes, and then to
 be
 able to kill them by a separate process if the apps take too long to run..

  You can have the  parent sleep, and then clean up like :

$aPids = array();
for ($i=0;$iCHILDREN_NUMBER;++$i)
{
if ( $iPid = pcntl_fork() == 0)
   {
 // children code

 exit(0);
   }
   else
   {
  // parent code
  $aPids[] = $iPid;
   }
}

// the parent sleeps
sleep(MAX_EXECUTION_OF_CHILDREN);

for ($i=0;$iCHILDREN_NUMBER;++$i)
{
// check how the child is doing
$iPid = int *pcntl_waitpid* ( $aChildren[$i], $iStatus , WNOHANG);

   if ($iPid == -1)
   {
  // oh these children ... newer doing what they should
  posix_kill($aChildren[$i]);
   }

}

this would also take care of zombies.



 So.. thoughts/comments would be appreciated!

 thanks



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] Re: process creation

2009-01-23 Thread Török Alpár
2009/1/23 Nathan Rixham nrix...@gmail.com

 bruce wrote:

 A simple question (or so I thought).

 Does php allow an app to create/start a process/application that can
 continue to run on its own, after the initiating program/app terminates?

 It appears that the spawning/forking functions might work, but the child
 apps would be in a zombie status, and couldn't be killed by an external
 program.


 you keep mentioning this zombie state; make sure that all you're child
 processes have an exit(); at the end or at the end of the code where they
 are finished; otherwise you get the xombies!

 also here is a very simple model you can follow that invariably works for
 me:

 this will run 10 worker threads:

 controller:
 ?php
 include './your.framework.php';
 for($icount=0;$icount11;$icount++)  {
include './worker.php';
 }
 ?

 worker:
 ?php
 $pid=pcntl_fork();
 if(!$pid) {
while(1) {
if($icount) {
$offset = $icount * 50;
} else {
$offset = 0;
}
$db = new mysql_handler( $connection );
$job_list = new job_list;
if( $jobs = $job_list-get($offset) ) {
foreach($jobs as $jdex = $job ) {
//do something with the job
}
} else {
sleep(10);
}
}
 } else {
echo \ndaemon launcher done id $pid\n;
 }
 ?

This would start more than 10 children. Children will continue on with for
loop after they do their work. As you advice that the children have an exit,
i assume that  you just overlooked it while writing this example. Also, a
wait on the children, at some point, gets rid of the zombies, as i see from
your code, there is no way you won't have zombie processes, unless the
parent exists, and then the zombies also disappear.

I hope i got it right, it's late here :)



 the above code is designed to run indefinately in a constant loop which
 polls a database for work to do

 this is just a very simple example, there are far more complex ways of
 doing it, keeping a track of how many processes you have, spawning new ones
 when you need them etc etc, but this i find works v well for me, the key is
 the $offset; getting jobs from a database and this literally is the offset
 used, so if you have say 200 emails to get and each script processes 50 at a
 time, only 4 of your threads are working, bump it up to 1 and all of
 them work until the queue drops; the sleep(10) and the spawn process of
 about 1 per second ensures that you're polling every second so jobs are
 picked up quickly. it's a lot of functionality for so little code :)



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] Re: process creation

2009-01-23 Thread Török Alpár
as i said it's hate here, and i might be wrong but consider the following  :

for($icount=0;$icount11;$icount++)
{
  $iPid  =  pcntl_fork();
  $iChildrenCount = 0;
  if ($iPid == 0)
  {
// child
echo (child $icount\n);
  }
  else
  {
// parrent
  }
}

this is essential what you do in your example? If so, this code does not
start 10 children. It starts more.

2009/1/23 Nathan Rixham nrix...@gmail.com

 Török Alpár wrote:

 2009/1/23 Nathan Rixham nrix...@gmail.com

  bruce wrote:

  A simple question (or so I thought).

 Does php allow an app to create/start a process/application that can
 continue to run on its own, after the initiating program/app terminates?

 It appears that the spawning/forking functions might work, but the child
 apps would be in a zombie status, and couldn't be killed by an external
 program.


  you keep mentioning this zombie state; make sure that all you're child
 processes have an exit(); at the end or at the end of the code where they
 are finished; otherwise you get the xombies!

 also here is a very simple model you can follow that invariably works for
 me:

 this will run 10 worker threads:

 controller:
 ?php
 include './your.framework.php';
 for($icount=0;$icount11;$icount++)  {
   include './worker.php';
 }
 ?

 worker:
 ?php
 $pid=pcntl_fork();
 if(!$pid) {
   while(1) {
   if($icount) {
   $offset = $icount * 50;
   } else {
   $offset = 0;
   }
   $db = new mysql_handler( $connection );
   $job_list = new job_list;
   if( $jobs = $job_list-get($offset) ) {
   foreach($jobs as $jdex = $job ) {
   //do something with the job
   }
   } else {
   sleep(10);
   }
   }
 } else {
   echo \ndaemon launcher done id $pid\n;
 }
 ?


 This would start more than 10 children. Children will continue on with for
 loop after they do their work. As you advice that the children have an
 exit,
 i assume that  you just overlooked it while writing this example. Also, a
 wait on the children, at some point, gets rid of the zombies, as i see
 from
 your code, there is no way you won't have zombie processes, unless the
 parent exists, and then the zombies also disappear.

 I hope i got it right, it's late here :)



 lol the script will only run 10 children, and as mentioned directly below,
 it is designed to run forever - the example doesn't fit the exact needs, but
 following bruces earlier posts this may be a model he can follow. I'm aware
 it could be fleshed out with much more code and error handling, but it's
 just a little model to get one started :)

 regards torak and hope you're well!


  the above code is designed to run indefinately in a constant loop which
 polls a database for work to do

 this is just a very simple example, there are far more complex ways of
 doing it, keeping a track of how many processes you have, spawning new
 ones
 when you need them etc etc, but this i find works v well for me, the key
 is
 the $offset; getting jobs from a database and this literally is the
 offset
 used, so if you have say 200 emails to get and each script processes 50
 at a
 time, only 4 of your threads are working, bump it up to 1 and all of
 them work until the queue drops; the sleep(10) and the spawn process of
 about 1 per second ensures that you're polling every second so jobs are
 picked up quickly. it's a lot of functionality for so little code :)



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php








-- 
Torok, Alpar Istvan


Re: [PHP] About printing functions

2009-01-22 Thread Török Alpár
2009/1/22 Jan G.B. ro0ot.w...@googlemail.com

 2009/1/21 Thodoris t...@kinetix.gr:
  ?php
ob_start();
   badFunctionThatSpitsInsteadOfReturning();
$sReturned = ob_get_contents();
   ob_end_clean();
  ?
 
  That's a good though thanks. Although I was aware of output buffering I
 used
  to ignore that ob_end_clean actually exists...
 You can even make it shorter by using
 ob_start(); echo 1;
 $retVar = ob_get_clean();

 http://de.php.net/ob_get_clean() http://de.php.net/ob_get_clean%28%29

Yes, but that leaves output buffering on, and that's a bad thing, especially
if you are on the console, but anyhow, why leave it on when you don't need
it, or even if you do, why create more levels?



 Byebye

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] Please explain: index.php/index/index

2009-01-21 Thread Török Alpár
2009/1/21 leledumbo leledumbo_c...@yahoo.co.id




 Carlos Medina-2 wrote:
 
  this is a Front Controller situation (Pattern)
 
 Could you explain more on that? I've never seen anything like this in any
 tutorial I've found on the net. I'm using Kohana framework. So, if I have
 index.php/index/index where does it actually go?

  it goes to index.php , apache disregards what's after index.php Kohana the
reads that string, and loads the appropriate Controller / method. You can
hide the index.php from your users via apaches mod_rewrite. Refere to the
Kohana or Codeigniter doccumentation


 --
 View this message in context:
 http://www.nabble.com/Please-explain%3A-index.php-index-index-tp21578728p21579231.html
 Sent from the PHP - General mailing list archive at Nabble.com.


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] Please explain: index.php/index/index

2009-01-21 Thread Török Alpár
Never used IIS, but under the circumstances, i think you got your answer. I
remember that codeigniter support many ways of getting that string, i
believe kohana does the same. You probably need to change some
configuration options for IIS

2009/1/21 leledumbo leledumbo_c...@yahoo.co.id


 Is this web server specific? I can't get it to run under Microsoft IIS, but
 it works flawlessly in Apache.
 --
 View this message in context:
 http://www.nabble.com/Please-explain%3A-index.php-index-index-tp21578728p21579384.html
 Sent from the PHP - General mailing list archive at Nabble.com.


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] About printing functions

2009-01-21 Thread Török Alpár
you can use Output Buffering :

?php
   ob_start();
  badFunctionThatSpitsInsteadOfReturning();
   $sReturned = ob_get_contents();
  ob_end_clean();
?

2009/1/21 Edmund Hertle edmund.her...@student.kit.edu

 2009/1/21 Jason Pruim japr...@raoset.com

 
  On Jan 21, 2009, at 1:33 PM, Thodoris wrote:
 
   Hi gang,
Lets say that you have a function that prints something to the output
  simply like this:
 
  function print_str() {
print blah blah blah;
  }
 
  I was wondering if there is a way to use this output and store it in a
 var
  or something without changing the function itself?
  And store the blah blah blah somewhere for later use?
 
  I can think of many reasons that someone could use this.
 
 
  I know you said without changing the function... but is there any reason
  that you can't simply add this:
 
  ?PHP
  function print_str() {
 $str = blah blah blah;
 print $str;
  }
  ?

 well, I think this will not work because $str is only valid in the function
 (local var!)




-- 
Torok, Alpar Istvan


Re: [PHP] About printing functions

2009-01-21 Thread Török Alpár
2009/1/21 Edmund Hertle edmund.her...@student.kit.edu

 2009/1/21 Thodoris t...@kinetix.gr

 
 
  On Jan 21, 2009, at 1:33 PM, Thodoris wrote:
 
   Hi gang,
Lets say that you have a function that prints something to the output
  simply like this:
 
  function print_str() {
print blah blah blah;
  }
 
  I was wondering if there is a way to use this output and store it in a
  var or something without changing the function itself?
  And store the blah blah blah somewhere for later use?
 
  I can think of many reasons that someone could use this.
 
 
  I know you said without changing the function... but is there any reason
  that you can't simply add this:
 
  ?PHP
  function print_str() {
 $str = blah blah blah;
 print $str;
  }
  ?
 
  Totally untested and just trying to understand why :)
 
 
  --
  Jason Pruim
  japr...@raoset.com
  616.399.2355
 
 
 
 
 
  Well Jason my point is theoretical. Lets just say that this function
  doesn't just print blah blah blah but like tones of html that you may
 like
  to reuse...
 

  Well you could always change it to this:
 
  function print_str() {
$str = blah blah blah;
return $str;
  }
 
  and use the output you got from the function whenever you like (print it,
  make it a toast, cook it with bees etc).
 
  But the question still remains: Is there a way to do this?
 
 
  --
  Thodoris
 

 IMO it is always better to return strings than directly printing. there
 would be no problem even if it would contain tones of html...

Agree, but the solution suggested might come in handy if you have some
template files that you just include, and you don't want to write all that
html inside php strings.



 but what comes in my mind is output_buffering...
 see: http://de3.php.net/manual/en/book.outcontrol.php

 -eddy




-- 
Torok, Alpar Istvan


Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Török Alpár
2009/1/21 Jay Moore jaymo...@accu-com.com


  I know it's very OO-y to use exceptions, but I hate them. They're like
 setjmp/longjmp calls in C, and they're a really headache to deal with.
 If you don't use default or predone handlers, you have to put all kinds
 of try/catch blocks around everything. They make for non-linear
 execution, and I prefer my code to execute in a linear fashion.

 this also means you use a single return in a function ?



 Paul


 My thoughts exactly.  What do I gain by using a try/catch that I lose by
 using if/else or similar?

the power to omit it, and then change your mind  lather, and handle them.
The only problem is that php itself (nor core, nor extensions) throw
exceptions, so you can't threat errors uniformly, but you cant have a custom
error handler, and throw exceptions from there, and you can also have an
exception handler. I have used this approach in several projects, and i
admit it takes some getting used to, but it's an easy, clean, nice way to
handle exceptions / errors / conditions.



 J

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] Client/Server Printing

2009-01-20 Thread Török Alpár
2009/1/20 Paul M Foster pa...@quillandmouse.com

 I'd like a side check on what I'm doing to print on our internal
 network.

 We have an internal server/site which uses PHP code I've written to
 run the business-- invoicing, A/P, inventory, etc. Some things, like
 invoices and reports, need to be printed. Now remember, the code is on
 the server, and we access it from our client machines on our desks. When
 we print, we do so to our local printers, attached to the client
 machines.

 So the best way I could think of to making printing work was to generate
 PDFs of whatever needs to be printed, dump the PDF on the server, and
 provide a link to the PDF on the web page. The user clicks on the
 generated PDF, and his/her browser opens up Acrobat or xpdf, and prints
 from that application to their local machine.

 Is that a reasonable way to implement this? Any better ideas?

 Paul

 --
 Paul M. Foster

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


You can use CSS for print media, and write the a different style sheet  for
the pages you want to print. You will have a page displayed with one  A.css
and you can have the browser automatically use B.css when print is
requested. No extra coding needed just the second css witch you link in the
header section of your html. The user only hits ctrl + P. You maight want to
place a note, that hey can print directly, and that, the print version will
look nice, they would probably expect it to print as is.

-- 
Torok, Alpar Istvan


Re: [PHP] PHP and Apache configuration

2009-01-19 Thread Török Alpár
2009/1/19 R B rbp...@gmail.com

 I like this apache solution, but if i put

 SetEnvIf Referer ^http://www.yourdomain.com; local_referal

 Then i can access the file putting this path in the URL:

  http://www.yourdomain.com/xyz/scriptfile.php
 And i don´t want the script to be access by the url. That is the main
 problem.

No, you can't , not if you do it properly. imagine that you have a folder
like /srv/www/htdocs that is the document root of you web server, and you
have /srv/www/includes, just annother file. You can have al your protected
files ther, and include them from files that are in the document root of
your web server, and are public. If you don't want this sepparation, you can
use a .htaccess file in the folder, and deny the folder from all. (i recall
hearing/reding that this actually works even if allow overrule is off ,
didn't actually tryed it, but i imagine is more of a hack )


 Thanks


 On Mon, Jan 19, 2009 at 1:38 PM, Richard Heyes rich...@php.net wrote:

   ...
 
  This may be of some help. It's from the Apache website and only allows
  access if the Referer header is sent by the browser and is
  www.yourdomain.com, ie. Direct access is not permitted:
 
  ###
  SetEnvIf Referer ^http://www.yourdomain.com; local_referal
 
  Order Deny,Allow
  Deny from all
  Allow from env=local_referal
  ###
 
  --
  Richard Heyes
 
  HTML5 Graphing for Firefox, Chrome, Opera and Safari:
  http://www.rgraph.org (Updated January 17th)
 




-- 
Torok, Alpar Istvan


Re: [PHP] developers life

2009-01-19 Thread Török Alpár
2009/1/19 Ashley Sheridan a...@ashleysheridan.co.uk

 On Mon, 2009-01-19 at 21:28 +, Nathan Rixham wrote:
  well just for the hell of it; and because I'm feeling worn..
 
  anybody else find the following true when you're a developer?
 
  - frequent bursts of side-tracking onto more interesting subjects
 yes, but it tends to be side-tracking to what management thinks is more
 important; which usually isn't!
  - vast amount of inhuman focus, followed by inability to remain focussed
 yes, and coffee can only go so far!
  - general tendancy to keep taking on projects, often for no good reason
 yes, I'm a sucker really, and can't seem to say no to people
  - inability to flip out of work mode at 5pm like the rest of the world
 yes, i work a lot on the commute to/from work :-/
  -- [sub] not feeling normal unless worked you've extra; while other
  professions demand overtime as little as an extra 15 minutes
  - constant learning (positive thing)
 yep
  - unlimited skill scope, if its on a computer you'll give it a go
 not too sure about actual skill scope, but like many a man, i'll give it
 a go, even if I know I don't know what I'm doing!
  - amazing ability to prioritise (the wrong things)
 yes, albeit aided immensely by management!
  - all projects suddenly become uninteresting and all motivation is lost
  at approx 65-85 percent completion
 yeah, funny that!
  - the code seems more important than the app (even though its not)
 what's usability again?
  - lots more but lost interest / focus
 hehe
 
  ps:
  expectantly installed windows 7 over the weekend, brief moment of
  excitement coupled with the thought i wonder what it's like;
  anticlimax + reality check, it was just a taskbar, start menu and
  desktop.. you'd think I'd know by now.
 go linux ;)

++1


 
  regards :-)
 
  i so have more important things to do
 


 Ash
 www.ashleysheridan.co.uk


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] optional type hinting enhancements

2009-01-18 Thread Török Alpár
I see a problem with this. Scalars are automatically casted by PHP based on
a set of rules. In  case of  a scalar type hint, would jo issue an error, or
make the automatic type cast?   both approaches have there advantages, but
the automatic cast, would go better with the actual features of the
language, but in this case, object could also cast an array to stdClass, and
i am not sure that's how you imagined it. Besides this i am not against it
along as it doens't create implementaion nor performance problems, and this
problem is solved.

2009/1/18 Nathan Rixham nrix...@gmail.com

 Hi All,

 preface: Having discussed at great length previously and probably
 completely misnaming and thus misleading the conversation here goes again.

 question: Would anybody else like to see, or feel the need for, *optional*
 type hinting of variables and class properties in PHP?

 examples (all optional, and syntax may differ):

 class Example {
  private TypeHint $var;
 }



 Example $var = new Example();


 in addition the ability to type hint primatives/scalars/[type object] in
 the existing implementation:

 function(bool $flag) {
 }



 function(object $flag) {
 }




 This would all be under the assumption and proviso that an implementation
 would not break bc, have any markable perfomance hit, or in any other way
 effect existing applications or new applications that did not use the
 functionality (in the same way the existing type hinting implementation
 doesn't)

 Any +1's?

 Regards.

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: [PHP] optional type hinting enhancements

2009-01-18 Thread Török Alpár
2009/1/19 Nathan Rixham nrix...@gmail.com

 Török Alpár wrote:

 I see a problem with this. Scalars are automatically casted by PHP based
 on
 a set of rules. In  case of  a scalar type hint, would jo issue an error,
 or
 make the automatic type cast?   both approaches have there advantages, but
 the automatic cast, would go better with the actual features of the
 language, but in this case, object could also cast an array to stdClass,
 and
 i am not sure that's how you imagined it. Besides this i am not against it
 along as it doens't create implementaion nor performance problems, and
 this
 problem is solved.


 I see what you mean with the casts; and a very good point on why scalars
 aren't already implemented..

 On the one hand i see scope for a set of primative wrappers (class String,
 Integer, Boolean etc) which those who wished could use (with an auto cast).

 Whilst on the other hand it could be argued that the current is_xxx
 functionality could be copied so an integer in a string is still a string,
 likewise 1 is an integer not a boolean true etc etc.

 defintaly needs some thought and practical examples though.. rfc time one
 thinks.

 with the specific array example, this is already implemented so no problems
 there, what is not implemented though is the ability to function(object
 $obj) which seems strange.

if a cast is made, and $obj is in fact an array, the outcome may not be what
you wanted. This one is even trickier, since you may expect an error here,
given that you have type hinting for specific object types.



 as for the error, same as it is currently E_RECOVERABLE_ERROR and def not
 automatic type casting; last thing you want when trying to be strict is
 anything like that :p

agree, but that would make static types behave differently than they
currently are. With some wrapper classes, you might end up with the same
trouble, you can type hint Integer, and send a scalar string that contains
an integer, then you are back to the good old casting rules.



 thanks for the input!


i think that this functionality is worth talking about, but the point is to
integrate this with the way PHP does things right now, you can't take the
Java model as is, and apply it to PHP, there is  a devil in the details.

-- 
Torok, Alpar Istvan


Re: [PHP] Cookie Question

2009-01-17 Thread Török Alpár
2009/1/17 PHP php_l...@ibcnetwork.net

 Hi,
 I am trying to get a cookie to set in Internet Explorer 7, I have tried
 several different setcookie() configurations, this is the latest.
 Yes, I read the manual and the user notes, but can't find anything specific
 about the different security levels in IE.

 $szCookieName = MyCookie;
 $nID = 2;
 $expireTime = 60*60;

 setcookie($szCookieName, $nID, time()-$expireTime,/,www.mysite.com
 ,false);


   is there any reason that you set the expire in the past? That is usually
used to delete a cookie, not set it. I had many problems with IE in general,
but cookies were never a problem.




 However, they all work, only if I have the Privacy slider set to low in
 IE's options.
 As soon as I go up to medium, it will not work.

 And it works fine with firefox.

 The only difference I can see is that Medium Security adds the rule:

 Restricts first-party cookies that save information that can be used to
 contact you without your implicit consent.

 All I am storing is an integer value, why is IE seeing that as information
 that can contact you?

 Thanks for any help.

 Chris




-- 
Torok, Alpar Istvan