Forum Moderators: coopster

Message Too Old, No Replies

While Loop ( Looping Twice )

         

kegham

4:04 pm on Jul 28, 2010 (gmt 0)

10+ Year Member



Hello everyone, i want to explain something happened with me recently. I have SMS broadcasting page on my hosting service, designed and coded by me. But the only problem recently ive experience it is that. I have created the page for sending BULK sms ( To more than 1 mobile numbers ). and end up with something really painful which is > While loop is looping twice while sending the SMS to those specified mobile numbers.

So here is what happening:
- The bulk SMS page ( Sending SMS form ) contain AJAX code.
- The form post go to the main SMS/API page which contain the while loop code.
- Ive figured out that the Ajax giving time out over 100 or more mobiles.

So my main question is:
Does AJAX time out, make the "While Loop", looping twice and be a reason to send the same message 2 times to the specified mobile numbers ? because when i check everything, ive saw that let say if i send 150SMS, to 150 mobile numbers, 300SMS are being broadcasted which is twice to each mobile number. There is a piece of code below for the main ( API & Loop ), which might be helpful regarding my post.



<?php

$mobile = array($_POST['GSM']); // Getting mobile numbers from a text area.
$values = explode("\n", $gsm[0]); // separating mobiles with line carriage
$header = $_POST['userID']; // Posting sms header.
$text = Urlencode($_POST['smstext']); // URLencoding the text.
$counter = 0; // Being loop
WHILE ($counter < count($values))
{
$url=("http://www.domain.com/sms.php/?UserId=myuser&txtPassword=mypass&GSM=$values[$counter]&Header=$header&Text=$text&Send=1");
$ret = file($url); // Broadcast the sms
$counter++;
}

?>

Matthew1980

7:00 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kegham,

First off welcome to the forum! [webmasterworld.com ]

your code:

You realise you are trusting the user to enter the phone number correctly & to only enter digits? Unless there is some form of sanitising & content checking that you perform somewhere else...

Personally I don't think as you need a while, you may only need a for loop:-

(I tweeked your code a little too ;))

<?php

$values = explode("\n", $_POST['GSM']); // separating mobiles with line carriage
$header = $_POST['userID']; // Posting sms header.
$text = urlencode($_POST['smstext']); // URLencoding the text.

//count array
$counted = count($values);//do the count outside the loop - less intensive on memory

for($i = 0; $i<$counted; $i++)
{
//do loop - this will only do the iterations according to the number of records in array
$url = "http://www.domain.com/sms.php/?UserId=myuser&txtPassword=mypass&GSM=$values[$i]&Header=$header&Text=$text&Send=1";
$ret = file($url); // Broadcast the sms
}
?>

Other than that, not too sure how this little piece of code operates as I haven't messed with sms stuff before. I just tried to simplify things and hopefully get the loop working...

Hope that gives you an alternative way to look at things anyway :)

Cheers,
MRb

kegham

10:03 pm on Jul 28, 2010 (gmt 0)

10+ Year Member



Hello Matthew and thanks for your reply dear. Yes user is entering the numbers correctly on plus of these all i do test it even and its the same. Now that ive test the code you provide it, another issue has been realized is that:

$counted = count($values);//do the count outside the loop - less intensive on memory

The above outside of the loop count, being a way only to take the first mobile number from the text area and send the same message to it twice, without passing to the rest numbers.

Matthew1980

10:23 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there KegHam,

Are you saying that even though I changed it slightly the output is still duplicate?

Debug time methinks.

for($i = 0; $i<$counted; $i++)
{
//echo the values to screen to see whats happening
echo $url = "http://www.domain.com/sms.php/?UserId=myuser&txtPassword=mypass&GSM=$values[$i]&Header=$header&Text=$text&Send=1</br>";
//$ret = file($url); // Broadcast the sms COMMENT this out until you have seen the output from the loop
}


That should show an url-per-line in your browser, from that you should see if there are any repetitions, if there aren't any, the the problem doesn't lye in the loop. As I said though, I'm not too sure how this: $ret = file($url); translates into an sms being sent, unless there is another script reading the file somewhere? Not sure I have never done sms stuff, I just tried to see if the loop was repeating itself - which I am fairly confident as it's not ;)

Cheers,
MRb

eelixduppy

1:31 am on Jul 29, 2010 (gmt 0)



Also, keep in mind that it might not be a bug on your part at all. Perhaps the third-party you are using to send the SMS is having issues of their own. Not likely, but possible if the problem escapes you from your side. Might at least be worth checking into.

kegham

5:52 pm on Jul 30, 2010 (gmt 0)

10+ Year Member



Well thanks for all replies:

Matthew:
I've try to echo the ($ret = file($url)) and everything was working fine and printing all result in good and right way.

eelixduppy:
I don't think there is a server ( Third party ) problem from the provider, because previously i try with other method and worked fine.

My answer and probably solution:
I think there is something related with the Ajax page which is the main page that include the SMS submitting form. For more easily access i do provided the complete page so that you can easily view its source and say me if may be there is something related with the Ajax time out.

Please check below link source code:
[protechzone.com...]