GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP advice on loops (https://gfy.com/showthread.php?t=555187)

V_RocKs 12-21-2005 04:59 PM

PHP advice on loops
 
I need to create a loop that do something.

If that something fails to happen, I need the loop to go back and do it again until it happens...

The basic:

top:
Get a URL that may timeout;
(if URL isn't grabbed) -> goto top;

My current understanding of loops in PHP can't figure out this kind of loop. Where it will keep going back into infinity when the if match isn't being met.

Anyone?

who 12-21-2005 05:02 PM

lookup 'while' on php.net :) It's really a great resource, and you'll thank yourself when you learn it properly.

SMG 12-21-2005 05:04 PM

do you mean something like this?

do {
$fp = fopen($url,'r');
$fout = '';
while ($s=fgets($fp,1000)) {
$fout .= $s
}
fclose($fp);
} while (empty($fout));

bangman 12-21-2005 05:07 PM

I know very little PHP, but from what I understand this is the correct structure:
PHP Code:

if (something happens)
  {
      Do 
this
  
}
  else
  {
      Do 
that
  
}


Though I have no idea how you would go about checking for a timeout etc

bangman 12-21-2005 05:08 PM

Ok, I was no help at all haha

V_RocKs 12-21-2005 05:10 PM

I am using cURL wrapped inside a function to get data from a URL... If it times out, I need it to ask for the data again. and again, until the function returns the data I want.

Sam Granger 12-21-2005 05:11 PM

Argf, hate when you gets tuck with php. I'm stuck too right now with an AJAX Live search script. :(

Gets results fine but i can't research without refreshing the page...

who 12-21-2005 05:15 PM

Quote:

Originally Posted by V_RocKs
I am using cURL wrapped inside a function to get data from a URL... If it times out, I need it to ask for the data again. and again, until the function returns the data I want.

This is the wrong way to go about any programming. You should set a limit on how many times your function makes the request. This makes writing the function much easier and removes your potential infinate loop (these are not acceptable in ordinary programming).

Sam Granger 12-21-2005 05:15 PM

Haha, yes!!! I solved my problem :) - well, someone else did :1orglaugh

function handleHttpResponse() {
if (http.readyState hahahaha 4 && http.status hahahaha 200) {
document.getElementById('results').innerHTML = http.responseText;
done = false;
}
}

SMG 12-21-2005 05:17 PM

Quote:

Originally Posted by V_RocKs
I am using cURL wrapped inside a function to get data from a URL... If it times out, I need it to ask for the data again. and again, until the function returns the data I want.

just use a similar tactic as what I showed, but with curl instead of fopen ... it should work
do {
grab your url with curl
} while (url hasnt been gotten);
is the basic concept/syntax

V_RocKs 12-21-2005 05:20 PM

Nice Sam... now...

The thing I guess I am not grasping is that the while statement is going to have to call the funtion in question and the check to see if it worked seems to come before that...
hmm...

OK, got it...

Fuck.. talk about hammering a server...

Baker Rd 12-21-2005 05:22 PM

why aren't you using a cron? what happens if the server is down and your script sits in a continuous loop until it eats enough memory to crash the box?

V_RocKs 12-21-2005 11:26 PM

Quote:

Originally Posted by Baker Rd
why aren't you using a cron? what happens if the server is down and your script sits in a continuous loop until it eats enough memory to crash the box?

I set a 10 try limit and a sleep in between each try... no continous loops for me.

phpslave 12-22-2005 12:21 AM

!!! Just a thought !!!

Maybe you should cache the results locally get the time before you enter a loop, (while loop or whatever doesn't matter) loop in it making your read requests, if you don't make the read in x amount of seconds, (1 sec would be a max time for me) read the cached local content.

It is very hard on your server to hang up apache on these long processes, I'd only read remotely if i had to. If it is a news feed or something you can get away normally with only having to read this in once or twice a day. The rest of the time reading from the local cached file you created.

In addition if you are pharsing the information you are reading in. Cache it in the form of already being pharse as pharsing is an expensive process as well.

If this site gets any type of traffic, a code written in such a way to keep trying if even for only 3 - 5 seconds could bring the server to its knees. So you need to think of other ways to achive your end result. With apache you only have so many processes you can use, generally around 250 apache processes on a server. After this, or close to this number you'll stop serving pages, aka loose your ass on your money. Long connection times can really cause this number to go much higher than needed and even spiral out of control, in general it's a really bad idea.

As mentioned by others if it keeps trying and you have no memory limit set and no script execution time set, the script could (and in my opinion at some time will) crash your server.

In addition about 'sleeping' in your php scripts this produces the above results, which are very harmful to your server. There is no reason to sleep in your php.


I don't know what problem you have, but you need to take a different look at it as what i see written here will not scale at all, and will greatly harm your server.

Lustmoney 12-22-2005 01:10 AM

Quote:

Originally Posted by bangman
I know very little PHP, but from what I understand this is the correct structure:
PHP Code:

if (something happens)
  {
      Do 
this
  
}
  else
  {
      Do 
that
  
}


Though I have no idea how you would go about checking for a timeout etc


:1orglaugh :1orglaugh :1orglaugh

V_RocKs 12-22-2005 02:29 PM

Quote:

Originally Posted by phpslave
!!! Just a thought !!!

Maybe you should cache the results locally get the time before you enter a loop, (while loop or whatever doesn't matter) loop in it making your read requests, if you don't make the read in x amount of seconds, (1 sec would be a max time for me) read the cached local content.

It is very hard on your server to hang up apache on these long processes, I'd only read remotely if i had to. If it is a news feed or something you can get away normally with only having to read this in once or twice a day. The rest of the time reading from the local cached file you created.

In addition if you are pharsing the information you are reading in. Cache it in the form of already being pharse as pharsing is an expensive process as well.

If this site gets any type of traffic, a code written in such a way to keep trying if even for only 3 - 5 seconds could bring the server to its knees. So you need to think of other ways to achive your end result. With apache you only have so many processes you can use, generally around 250 apache processes on a server. After this, or close to this number you'll stop serving pages, aka loose your ass on your money. Long connection times can really cause this number to go much higher than needed and even spiral out of control, in general it's a really bad idea.

As mentioned by others if it keeps trying and you have no memory limit set and no script execution time set, the script could (and in my opinion at some time will) crash your server.

In addition about 'sleeping' in your php scripts this produces the above results, which are very harmful to your server. There is no reason to sleep in your php.


I don't know what problem you have, but you need to take a different look at it as what i see written here will not scale at all, and will greatly harm your server.

Thanks for the input... Definately going to look into cacheing

Libertine 12-22-2005 02:38 PM

If this is a mission-critical script, one piece of advice: don't do it yourself. Stuff like this, when done wrong, is exactly what causes huge server load, infinite loops, dead pages, etc. It seems to me that you aren't very experienced in PHP yet, so if you aren't just doing this as some exercise but as something essential to one of your sites, getting a real programmer is probably a good idea.

V_RocKs 12-22-2005 02:44 PM

Quote:

Originally Posted by punkworld
If this is a mission-critical script, one piece of advice: don't do it yourself. Stuff like this, when done wrong, is exactly what causes huge server load, infinite loops, dead pages, etc. It seems to me that you aren't very experienced in PHP yet, so if you aren't just doing this as some exercise but as something essential to one of your sites, getting a real programmer is probably a good idea.

I would agree that I am not good at programming PHP except that my meager abilities make me $9000 a month in affiliate income and another $4000 a month consulting and programming for others...

:2 cents:

This server in question BTW will not be dead on the amount of traffic it is getting...

Libertine 12-22-2005 02:50 PM

Quote:

Originally Posted by V_RocKs
I would agree that I am not good at programming PHP except that my meager abilities make me $9000 a month in affiliate income and another $4000 a month consulting and programming for others...

:2 cents:

This server in question BTW will not be dead on the amount of traffic it is getting...

No need to get all defensive. I was offering some serious advice based on your post.

You said:
Quote:

My current understanding of loops in PHP can't figure out this kind of loop. Where it will keep going back into infinity when the if match isn't being met.
I don't think it's weird to assume that you're new to php based on that statement...

V_RocKs 12-22-2005 04:23 PM

Quote:

Originally Posted by punkworld
No need to get all defensive. I was offering some serious advice based on your post.

You said:


I don't think it's weird to assume that you're new to php based on that statement...

Not getting defensive... and I asked that question so I wouldn't have to look shit up... I also was having a brain fart... I should have run the function first, then tested the loop on its outcome, while also testing on the function being run inside the loop...


All times are GMT -7. The time now is 07:15 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123