|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 > |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 > |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|