Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 12-21-2005, 04:59 PM   #1
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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?
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:02 PM   #2
who
So Fucking Banned
 
Join Date: Aug 2003
Location: ICQ #23642053
Posts: 19,593
lookup 'while' on php.net It's really a great resource, and you'll thank yourself when you learn it properly.
who is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:04 PM   #3
SMG
Confirmed User
 
Join Date: Aug 2003
Posts: 1,798
do you mean something like this?

do {
$fp = fopen($url,'r');
$fout = '';
while ($s=fgets($fp,1000)) {
$fout .= $s
}
fclose($fp);
} while (empty($fout));
__________________
TGP Webmasters: sign up for the top 100 tgp list!
Submit galleries
If you add me to icq (title) make sure to mention GFY or I'll think you're a bot and deny you.
SMG is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:07 PM   #4
bangman
Confirmed User
 
Join Date: Sep 2004
Location: PA, USA
Posts: 5,283
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
__________________
- David Hall
ICQ: 312744199
bangman is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:08 PM   #5
bangman
Confirmed User
 
Join Date: Sep 2004
Location: PA, USA
Posts: 5,283
Ok, I was no help at all haha
__________________
- David Hall
ICQ: 312744199
bangman is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:10 PM   #6
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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.
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:11 PM   #7
Sam Granger
Confirmed User
 
Sam Granger's Avatar
 
Join Date: Dec 2004
Location: NL (Eindhoven), CZ(Prague), FR(Concarneau)
Posts: 3,958
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...
__________________
[img]http://****************/sig/fhv3_j2_624x80_2.gif[/img]
$35-40 Per Signup, 60-70% Rev Share, over 80 Sites, Exclusive Sites, tons of free content
14,000+ Free hosted Galleries, RSS feeds, Domain Hosting, Embedded Flash Movies
Join Fetish Hits now!
ICQ: 358652230
Sam Granger is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:15 PM   #8
who
So Fucking Banned
 
Join Date: Aug 2003
Location: ICQ #23642053
Posts: 19,593
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).
who is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:15 PM   #9
Sam Granger
Confirmed User
 
Sam Granger's Avatar
 
Join Date: Dec 2004
Location: NL (Eindhoven), CZ(Prague), FR(Concarneau)
Posts: 3,958
Haha, yes!!! I solved my problem - well, someone else did

function handleHttpResponse() {
if (http.readyState hahahaha 4 && http.status hahahaha 200) {
document.getElementById('results').innerHTML = http.responseText;
done = false;
}
}
__________________
[img]http://****************/sig/fhv3_j2_624x80_2.gif[/img]
$35-40 Per Signup, 60-70% Rev Share, over 80 Sites, Exclusive Sites, tons of free content
14,000+ Free hosted Galleries, RSS feeds, Domain Hosting, Embedded Flash Movies
Join Fetish Hits now!
ICQ: 358652230
Sam Granger is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:17 PM   #10
SMG
Confirmed User
 
Join Date: Aug 2003
Posts: 1,798
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
__________________
TGP Webmasters: sign up for the top 100 tgp list!
Submit galleries
If you add me to icq (title) make sure to mention GFY or I'll think you're a bot and deny you.
SMG is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:20 PM   #11
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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...
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 05:22 PM   #12
Baker Rd
Confirmed User
 
Join Date: Nov 2005
Posts: 647
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?
__________________
money talks and bullshit walks.
Baker Rd is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-21-2005, 11:26 PM   #13
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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.
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 12:21 AM   #14
phpslave
Confirmed User
 
Join Date: Jan 2003
Location: San Jose
Posts: 548
!!! 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.
__________________

phpslave is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 01:10 AM   #15
Lustmoney
Registered User
 
Join Date: Nov 2005
Posts: 17
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

Lustmoney is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 02:29 PM   #16
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 02:38 PM   #17
Libertine
sex dwarf
 
Libertine's Avatar
 
Join Date: May 2002
Posts: 17,860
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.
__________________
/(bb|[^b]{2})/
Libertine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 02:44 PM   #18
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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...



This server in question BTW will not be dead on the amount of traffic it is getting...
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 02:50 PM   #19
Libertine
sex dwarf
 
Libertine's Avatar
 
Join Date: May 2002
Posts: 17,860
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...



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...
__________________
/(bb|[^b]{2})/
Libertine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-22-2005, 04:23 PM   #20
V_RocKs
Damn Right I Kiss Ass!
 
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
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...
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.