php-general Digest 19 Aug 2009 07:52:37 -0000 Issue 6293

Topics (messages 296941 through 296952):

Really quick try/catch question
        296941 by: Brian Dunning
        296942 by: Nathan Nobbe

PHP and CGI
        296943 by: sono-io.fannullone.us
        296944 by: Daevid Vincent
        296945 by: Shawn McKenzie
        296946 by: Shawn McKenzie
        296948 by: sono-io.fannullone.us
        296949 by: kranthi

Re: daemon without pcntl_fork
        296947 by: Lars Torben Wilson
        296952 by: Jim Lucas

Re: Undefined Offset Error with Pagination and Sessions
        296950 by: kranthi

Re: getting the search words from a google query
        296951 by: kranthi

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

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


----------------------------------------------------------------------
--- Begin Message ---
I have a "new SimpleXMLElement()" that is occasionally throwing:
'Exception: String could not be parsed as XML'

Will this catch it when it happens, or am I missing something?

function domyfunction() {
        // This does some stuff to exit the script gracefully
}

try {
        $xmlobject = new SimpleXMLElement($xml);
} catch($e) {
        domyfunction($e);
}



--- End Message ---
--- Begin Message ---
On Tue, Aug 18, 2009 at 1:29 PM, Brian Dunning <br...@briandunning.com>wrote:

> I have a "new SimpleXMLElement()" that is occasionally throwing:
> 'Exception: String could not be parsed as XML'
>
> Will this catch it when it happens, or am I missing something?
>
> function domyfunction() {
>        // This does some stuff to exit the script gracefully
> }
>
> try {
>        $xmlobject = new SimpleXMLElement($xml);
> } catch($e) {
>        domyfunction($e);
> }


you need to add the name of the class you want to capture, or an ancestor
thereof,

try {
  ..
} catch(Exception $e) {
 ..
}

-nathan

--- End Message ---
--- Begin Message --- I've searched high and low for an answer to this. Hopefully someone here might know. Can PHP be used under a CGI? I tried to put the following code on one of my perl shopping cart pages but it doesn't work:

<?php
echo "<select name=\"Year\">";
for ($year = date("Y") ; $year <= date("Y") + 8 ; $year++) {
        echo "<option value=\"$year\">$year</option>\n";
}
echo "</select>";
?>

It works fine on a .php page. I know that SSI will not work under CGI, so maybe it's the same for PHP. Is there anyway to get this to work?

Thanks,
Frank

--- End Message ---
--- Begin Message ---
 

> -----Original Message-----
> From: sono...@fannullone.us [mailto:sono...@fannullone.us] 
> Sent: Tuesday, August 18, 2009 5:10 PM
> To: PHP General List
> Subject: [PHP] PHP and CGI
> 
>       I've searched high and low for an answer to this.  
> Hopefully someone  
> here might know.  Can PHP be used under a CGI?  I tried to put the  
> following code on one of my perl shopping cart pages but it doesn't  
> work:
> 
> <?php
> echo "<select name=\"Year\">";
> for ($year = date("Y") ; $year <= date("Y") + 8 ; $year++) {
>       echo "<option value=\"$year\">$year</option>\n";
> }
> echo "</select>";
> ?>
> 
>        It works fine on a .php page.  I know that SSI will 
> not work under  
> CGI, so maybe it's the same for PHP.  Is there anyway to get this to  
> work?

Well, if you already have the page coded in Perl, why not just write those 5
lines in perl?!

It's a straight up for loop basically and some print statements. You're not
hitting a database or using anything remotely PHP-esque there.

I'm not so sure you can just mix and match parsers. I recall years ago some
talk of doing this, but I think it's just so rare and really impractical
that it's not worth setting up even if you can do it with some Apache magic.
Stick to one pre-processor language. Either all PHP or all Perl but not
both.

Daevid.

"Some people, when confronted with a problem, think 'I know, I'll use XML.'"
Now they have two problems. 


--- End Message ---
--- Begin Message ---
sono...@fannullone.us wrote:
>     I've searched high and low for an answer to this.  Hopefully someone
> here might know.  Can PHP be used under a CGI?  I tried to put the
> following code on one of my perl shopping cart pages but it doesn't work:
> 
> <?php
> echo "<select name=\"Year\">";
> for ($year = date("Y") ; $year <= date("Y") + 8 ; $year++) {
>     echo "<option value=\"$year\">$year</option>\n";
> }
> echo "</select>";
> ?>
> 
>      It works fine on a .php page.  I know that SSI will not work under
> CGI, so maybe it's the same for PHP.  Is there anyway to get this to work?
> 
> Thanks,
> Frank

PHP can run under CGI, however you're asking about mixing PHP with Perl.
 CGI != Perl.  CGI is the interface that your scripts have to the
programs that run them (i.e. Perl or PHP, etc...).

I've never done it, but to have both Perl and PHP parsed in one file,
you'd have to configure Apache (or your webserver) to parse the file
using PHP and then Perl.  Not sure if it would do this sequencially though.

Another option may be to put the PHP in its own PHP file and then fetch
it into your Perl script.

-- 
Thanks!
-Shawn
http://www.spidean.com

--- End Message ---
--- Begin Message ---

Andrew Mason wrote:
> If you already have it written in Perl, I would recommend writing the
> rest of functionality in Perl.
>
>   
Please reply all.  I agree, but I assumed that the problem was that he
knew PHP and he had downloaded a Perl cart and didn't know Perl.

-Shawn

--- End Message ---
--- Begin Message ---

On Aug 18, 2009, at 7:13 PM, Shawn McKenzie wrote:

I assumed that the problem was that he
knew PHP and he had downloaded a Perl cart and didn't know Perl.

That's exactly the case. I have been running my business on a Perl cart for the last 5+ years, and I can't switch to a PHP cart just yet. I was just hoping to add some functionality with PHP. Perl was much harder for me to grasp than PHP, and from what I can tell, you can't embed Perl in HTML as easy as you can with PHP, so I've switched languages. I guess I was hoping for too much. =:(

Thanks,
Frank

P.S.  Does anyone know of a good Perl mailing list?

--- End Message ---
--- Begin Message ---
try adding

AddType application/x-httpd-php .pl --> or whatever extension your
perl script has

--- End Message ---
--- Begin Message ---
2009/8/18 Per Jessen <p...@computer.org>:
> Lars Torben Wilson wrote:
>
>> Again, that's not a daemon. If it is sufficient to run a background
>> process then that's fine, but that doesn't make it a daemon (although
>> it shares some things in common with a daemon). The background process
>> still has a controlling terminal (even if input and output have been
>> redirected), and a fully-implemented daemon will typically perform
>> other tasks which make it a good system citizen, such as: becoming
>> session leader, chroot'ing to / so that the filesystem it was started
>> from can be unmounted if necessary, and so on.
>
> Torben, you're really just splitting hairs - the OP didn't ask for the
> definition of "daemon', he just wanted a script to run continually as
> if it was a daemon.  Besides, I did also suggest using either setsid or
> openSUSEs startproc.

Thank you for the lecture. Your opinion of my post on this is quite
beside the point. If Jim needs a daemon, then he should probably use a
daemon coded the best way possible. If not, then that's fine too. As
you correctly pointed out, there are ways to fake it. I simply feel
that Jim should be made aware of potential drawbacks. That's perhaps
splitting hairs--it's also being thorough. Clients don't tend to
appreciate being told that a lack of thoroughness arose from a fear of
splitting hairs.


Regards,

Torben

--- End Message ---
--- Begin Message ---
Lars Torben Wilson wrote:
2009/8/18 Per Jessen <p...@computer.org>:
Jim Lucas wrote:

Does anybody know how to use PHP as a daemon without the use of
pcntl_fork.

Sure. Just start it and leave it running.

I want to launch a daemon out of the /etc/rc.local when the system
starts.
Yep, I do that all the time.

Anybody have any idea on how to do this?
On an openSUSE system I would just use 'startproc', but elsewhere you
could use setsid or nohup.  I.e. create your CLI script with a
hash-bang, then start it

nohup <script> 2>&1 1>/dev/null &


/Per

Again, that's not a daemon. If it is sufficient to run a background
process then that's fine, but that doesn't make it a daemon (although
it shares some things in common with a daemon). The background process
still has a controlling terminal (even if input and output have been
redirected), and a fully-implemented daemon will typically perform
other tasks which make it a good system citizen, such as: becoming
session leader, chroot'ing to / so that the filesystem it was started
from can be unmounted if necessary, and so on.

The difference may or may not be important, depending on the task at
hand. However, if an admin thinks he's got a daemon on his hands he
may wonder why things behave weird if what he really has is just
something which fakes it with & and nohup, since none of the important
daemon housekeeping has been dealt with.


Torben


Thanks for all the input you two!

Currently, I have two different uses for this startup script and daemon/bg 
process.

One will deal with a master server for one of my games and the second will be for running a PBX SMDR/CDR capturing tool.

The first must be able to deal with multiple simultaneous connections where as the second simply need to listen on the local serial port, connect to a remote IP:PORT, or attach itself to a IP:PORT and wait for input from another machine to be sent to it. Once it receives the data, it simply parses it, sanitizes it, and stores it.

BUT ANYWAYS...

My system is OpenBSD 4.3 with PHP 5.2.8

Here is what I have come up with so far.  Looks to satisfying my needs:

tms_daemon
#! /bin/sh
#
# tms_daemon
#
#    Starts a listening daemon that acts as a Tribes Master Server
#
# Author:  Jim Lucas <jlu...@cmsws.com>
#
# Version:  0.0.1
#

set -e

DESC="Tribes Master Server Daemon"
DAEMON=/path/to/file/tms-0.1.5.php
PIDFILE=/var/run/tms.pid
SCRIPTNAME=/path/to/file/tms_daemon

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

#
#  Function that starts the daemon/service.
#
d_start() {
  if [ -f $PIDFILE ]; then
    echo "$DESC already running: PID# `cat $PIDFILE`"
    exit 1
  else
    echo -n "Starting $DESC"
    nohup $DAEMON 2>&1 1>/dev/null &
    sleep 0.5
    ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}' > $PIDFILE
    echo ". [`cat $PIDFILE`]"
  fi
}

#
#  Function that stops the daemon/service.
#
d_stop() {
  if [ -f $PIDFILE ]; then
    echo -n "Stopping $DESC"
    kill `cat $PIDFILE`
    rm $PIDFILE
    echo "."
  else
    echo "$DESC is not running"
    exit 1
  fi
}

case "$1" in
  start)
    d_start
  ;;
  stop)
    d_stop
  ;;
  status)
    if [ -f $PIDFILE ]; then
      echo "$DESC is running: PID# `cat $PIDFILE`"
    else
      echo "$DESC is not running"
    fi
  ;;
  restart|force-reload)
    d_stop
    sleep 0.5
    d_start
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 1
  ;;
esac

exit 0
# END OF SCRIPT

Well, I realize this probably doesn't have much to do /directly/ with PHP, but it is a good exercise anyways.

I can start this from /etc/rc.local or from the cli and it does the same thing 
either way.

Comments and suggestions are welcome.  Please let me know if I can improve this 
at all.

TIA!

--
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 ---
may be you can use a couple of var_dumps to see what's happening
behind the screens.


and... use a debugger like xdebug.. it'll be of help

--- End Message ---
--- Begin Message ---
can you provide a sample URL that you have in your mysql database ?

--- End Message ---

Reply via email to