PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > newbie questions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
newbie questions

Réponse
 
LinkBack Outils de la discussion
Vieux 21/10/2007, 06h17   #1
Ravi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut newbie questions


Guys, I am fairly new to PHP. Here are a few questions, if anybody can
answer it will me get started. Thanks

I am trying to build a website and I would like to do the following in
my scripts

1. I want to return response to the browser and AFTERWARDS make a log
entry in to a database. I need this so user can experience a fast response.

2. If the database update fails, I want to ignore it (since it is just
log entry). Something like try-catch construct in Java. This is more
important if item1 mentioned above is not possible. Essentially whether
I make a database entry or not, I must return a valid response to user.

3. Is there something like connection pool in php? Do usually people
open/close database connection for every request (I doubt that, it
sounds really slow).


Some code samples or pointers to documentation for the above would also
be very ful.

Thanks
Ravi
  Réponse avec citation
Vieux 21/10/2007, 07h44   #2
Floris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie questions

Ravi schreef:

> 1. I want to return response to the browser and AFTERWARDS make a log
> entry in to a database. I need this so user can experience a fast response.


This is not possible, because in php you usually have no control when
the generated html is sent to the browser. You can add your log code at
the end of your script, but there is no garantuee that that code will be
run after html is sent. I wouldn't worry about this too much. MySQL
access is very fast.

> 2. If the database update fails, I want to ignore it (since it is just
> log entry). Something like try-catch construct in Java. This is more
> important if item1 mentioned above is not possible. Essentially whether
> I make a database entry or not, I must return a valid response to user.


Php 5 has exception handling:
http://www.php.net/manual/en/language.exceptions.php

> 3. Is there something like connection pool in php? Do usually people
> open/close database connection for every request (I doubt that, it
> sounds really slow).


Opening a database connection for every webpage is actually not slow at
all. But if you are worried there is something like a persistent connect
that you can keep open across requests. See:
http://www.php.net/manual/en/functio...l-pconnect.php

Floris
  Réponse avec citation
Vieux 21/10/2007, 09h27   #3
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie questions

Ravi wrote:
>
> Guys, I am fairly new to PHP. Here are a few questions, if anybody can
> answer it will me get started. Thanks
>
> I am trying to build a website and I would like to do the following in
> my scripts
>
> 1. I want to return response to the browser and AFTERWARDS make a log
> entry in to a database. I need this so user can experience a fast response.

There is no "before and after". Everything you do happens during (part
of) the response. But you can just output your data, whatever it may be,
flush() it and then log it via the same script. Your user won't notice a
thing (Hell, even without the flush your user won't notice it probably).

> 2. If the database update fails, I want to ignore it (since it is just
> log entry). Something like try-catch construct in Java. This is more
> important if item1 mentioned above is not possible. Essentially whether
> I make a database entry or not, I must return a valid response to user.

So ignore it If you don't check for errors, you won't see them...
Makes debugging very annoying, but you won't see em nevertheless. If
your output is not based on anything from your database-update, then
there apparently is no need to worry about it.

> 3. Is there something like connection pool in php? Do usually people
> open/close database connection for every request (I doubt that, it
> sounds really slow).

There is something like that, the persistent connections (ie. via
mysql_pconnect), but generally people DO open/close connections via the
same script each and every time the script is executed (this might sound
very slow, but it's actually not too bad). Using persistent connections
is not always the best option (and usually doesn't even make much
sense); there's a good bit of documentation about it in the php docs:
http://www.php.net/manual/en/feature...onnections.php

> Some code samples or pointers to documentation for the above would also
> be very ful.

code samples of what exactly ?

> Thanks
> Ravi

  Réponse avec citation
Vieux 21/10/2007, 13h53   #4
Ravi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie questions


That was very very ful. Thanks a ton!

One more question. For every request, I am sending a redirect back to
the user and the browser takes the user to another url. The problem is
that the browser is not redirecting until the script finishes. Even if I
do flush(), the browser waits til script ends. Is there a way to force
browser to redirect and not wait for the script to end?

In Java I can think of many ways, one is to use threads, hand of data to
another thread and return the response. Another solution would be to
store data in memory (static variable) and update only after every 100
requests.

Is any of this possible in PHP?


M. Sokolewicz wrote:
> Ravi wrote:
>>
>> Guys, I am fairly new to PHP. Here are a few questions, if anybody can
>> answer it will me get started. Thanks
>>
>> I am trying to build a website and I would like to do the following in
>> my scripts
>>
>> 1. I want to return response to the browser and AFTERWARDS make a log
>> entry in to a database. I need this so user can experience a fast
>> response.

> There is no "before and after". Everything you do happens during (part
> of) the response. But you can just output your data, whatever it may be,
> flush() it and then log it via the same script. Your user won't notice a
> thing (Hell, even without the flush your user won't notice it probably).
>
>> 2. If the database update fails, I want to ignore it (since it is just
>> log entry). Something like try-catch construct in Java. This is more
>> important if item1 mentioned above is not possible. Essentially
>> whether I make a database entry or not, I must return a valid response
>> to user.

> So ignore it If you don't check for errors, you won't see them...
> Makes debugging very annoying, but you won't see em nevertheless. If
> your output is not based on anything from your database-update, then
> there apparently is no need to worry about it.
>
>> 3. Is there something like connection pool in php? Do usually people
>> open/close database connection for every request (I doubt that, it
>> sounds really slow).

> There is something like that, the persistent connections (ie. via
> mysql_pconnect), but generally people DO open/close connections via the
> same script each and every time the script is executed (this might sound
> very slow, but it's actually not too bad). Using persistent connections
> is not always the best option (and usually doesn't even make much
> sense); there's a good bit of documentation about it in the php docs:
> http://www.php.net/manual/en/feature...onnections.php
>
>> Some code samples or pointers to documentation for the above would
>> also be very ful.

> code samples of what exactly ?
>
>> Thanks
>> Ravi

>

  Réponse avec citation
Vieux 21/10/2007, 14h04   #5
Richard Heyes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: newbie questions

Ravi wrote:
>
> That was very very ful. Thanks a ton!
>
> One more question. For every request, I am sending a redirect back to
> the user and the browser takes the user to another url. The problem is
> that the browser is not redirecting until the script finishes. Even if I
> do flush(), the browser waits til script ends. Is there a way to force
> browser to redirect and not wait for the script to end?
>
> In Java I can think of many ways, one is to use threads, hand of data to
> another thread and return the response. Another solution would be to
> store data in memory (static variable) and update only after every 100
> requests.


Not having read the rest of the thread, you could call exit just after
the redirect header is sent, eg:

<?php
header('Location: http://www.yahoo.com');
exit;
?>

Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and Desk software
that can cut the cost of online support
  Réponse avec citation
Vieux 21/10/2007, 14h41   #6
Ravi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: newbie questions


Richard, unfortunately I cannot end the script. I need something like this:

<?php
header('Location: http://www.yahoo.com');
// somehow let the browser move to yahoo.com
// now update the database to store some information about user
exit;
?>


Richard Heyes wrote:
> Ravi wrote:
>>
>> That was very very ful. Thanks a ton!
>>
>> One more question. For every request, I am sending a redirect back to
>> the user and the browser takes the user to another url. The problem is
>> that the browser is not redirecting until the script finishes. Even if
>> I do flush(), the browser waits til script ends. Is there a way to
>> force browser to redirect and not wait for the script to end?
>>
>> In Java I can think of many ways, one is to use threads, hand of data
>> to another thread and return the response. Another solution would be
>> to store data in memory (static variable) and update only after every
>> 100 requests.

>
> Not having read the rest of the thread, you could call exit just after
> the redirect header is sent, eg:
>
> <?php
> header('Location: http://www.yahoo.com');
> exit;
> ?>
>
> Richard Heyes
> +44 (0)800 0213 172
> http://www.websupportsolutions.co.uk
>
> Knowledge Base and Desk software
> that can cut the cost of online support
>

  Réponse avec citation
Vieux 21/10/2007, 14h52   #7
Richard Heyes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: newbie questions

Ravi wrote:
>
> Richard, unfortunately I cannot end the script. I need something like this:
>
> <?php
> header('Location: http://www.yahoo.com');
> // somehow let the browser move to yahoo.com
> // now update the database to store some information about user
> exit;
> ?>


In that case you might want to look at register_shutdown_function().

Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and Desk software
that can cut the cost of online support
  Réponse avec citation
Vieux 21/10/2007, 17h52   #8
Larry Garfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: newbie questions

On Sunday 21 October 2007, Richard Heyes wrote:
> Ravi wrote:
> > Richard, unfortunately I cannot end the script. I need something like
> > this:
> >
> > <?php
> > header('Location: http://www.yahoo.com');
> > // somehow let the browser move to yahoo.com
> > // now update the database to store some information about user
> > exit;
> > ?>

>
> In that case you might want to look at register_shutdown_function().


That would work, but I think you're probably not approaching the question
properly. Why do you need to redirect the user first, then log the request?
PHP/MySQL are fast enough that logging first and then redirecting will have
no noticeable impact on performance or your user experience. (I'm assuming a
logging process here that's only 1-3 queries.) It sounds like you're trying
to over-optimize, which is always a bad idea as it makes the code harder to
understand later. :-)

--
Larry Garfield AIM: LOLG42
larry@garfieldtech.com ICQ: 6817012

"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h18.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20737 seconds with 16 queries