Jump to content

[SOLVED] Problem with arrays


iAreCow

Recommended Posts

So I have a tiny problem with arrays, basically I have to store 239 values in an array, later use foreach(); to process them all. I have the last part working but there are complications with the first one.

 

1. I submit values using textarea, putting each value on each row, then later I explode(); to get the values into an array. If I print_r();, everything looks fine, arrays from 0-239 show up.

<form method="POST">
UserIDs: <textarea name="userids" cols="75" rows"100"></textarea>
<input name="submit" type="submit" />
</form>
<?php
set_time_limit(0);
ignore_user_abort(true);

$ids = $_POST['userids'];
$exp = explode("\n",$ids);
print_r($exp);
// The values show up correctly when print_r();'ing them

foreach ($exp as $uinfo) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"http://sitetovisit.com/users.php?id={$uinfo}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec ($ch);
        curl_close ($ch);
        }
?>

Yet, after processing all of them, the server replies with error 400 from 0-238, yet, 239th value is processes correctly.

 

2. This one works just fine (all array values get processed without 400 server errors), but Im missing a feature here - textarea submitting (yes this one has only 3 values in the array, but Im too lazy to do more.)

<?php
set_time_limit(0);
ignore_user_abort(true);

// $ids = $_POST['userids'];
// $exp = explode("\n",$ids);
// print_r($exp);

$ids = array("1662683", "2040182", "1690671");
print_r($ids);

foreach ($ids as $uinfo) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"http://sitetovisit.com/users.php?id={$uinfo}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec ($ch);
        curl_close ($ch);
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/179667-solved-problem-with-arrays/
Share on other sites

Cant be true, this script does about the same work (just using range(); ) and works just fine.

 

<form method="POST">
UserID from: <input name="from" value="2038700" size="50" /><br>
UserID to: <input name="to" value="2133012" size="50" /><br>
<input name="submit" type="submit" />
</form>
<?php
set_time_limit(0);
ignore_user_abort(true);

if (!isset($_POST['submit'])) die();

$from = $_POST['from'];
$to = $_POST['to'];

foreach (range($from, $to) as $uinfo) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"http://sitetovisit.com/users.php?id={$uinfo}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec ($ch);
        curl_close ($ch);
        }
?>

My guess is the page breaks are \r\n, so when you break on \n there are still \r's on the end of the line which is breaking the request, the last line works because there are no page breaks after it. Swap your explode statement to split on \r\n.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.