Jump to content

lookup data from text file and removing a line problem


cobusbo

Recommended Posts

Hi I've created a scricpt thats devided into 4 parts

Part 1

Inserting info into a text file

Part 2

Removing info from the text file

Part 3

Lookup the value stored within the text file if it match show form if not don't show form

Part 4

Broadcasting the message

 

 

I'm currently experiencing a problem with Part 2. Every time I click the button it doesn't remove the info needed after a few clicks the whole unique.csv file gets removed

<html>
<head>
</head>
<body>
Welcome To The Chat!

To enter please Click <a href=\?func=yes>HERE</a>
To exit please Click <a href=\?func=no>HERE</a>

<?
//Part 1
if($_GET['func']=='yes') {

$myfile = fopen("users.csv", "a+") or die("Unable to open file!");
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];
if(!isset($mxituid))
{
	$mxituid = "Debater";
}

$txt = "$mxituid\n";
fwrite($myfile, $txt);
fclose($myfile);


$list = file('users.csv');
$list = array_unique($list);
file_put_contents('unique.csv', implode('', $list));
 
header("Location: index.php");
    exit;
  }

//Part 2
if($_GET['func']=='no') {
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];
$source = "unique.csv";
$raw = file_get_contents($source) or die("Cannot read file");
$wordlist = $mxituid;
$raw = preg_replace($wordlist, "", $raw);
file_put_contents($source, $raw);

 
header("Location: index.php");
    exit;




  }

//Part 3
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];
$file = 'unique.csv';
$searchfor = $mxituid;


// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
print "Type in your message<br>";   
print <<<HERE
<form method="post" action="index.php">
<input type = "text"
name = "message"
value = "">
<input type="submit"
value="Guess">
</form>
HERE;

}
else{
   echo "You should Enter Chat first!";
}

//Part 4
$visitor = 'unique.csv';

$f_pointer=fopen($visitor,"r"); // file pointer
$users = array();
while ($ar=fgetcsv($f_pointer)) {
    if ($ar[0] != '') $users[] = $ar[0]; //only store line in new array if it contains something
}
fclose ($f_pointer);

$batchSize = 50;                          // set size of your batches

$batches = array_chunk($users, $batchSize);


require_once ('MxitAPI.php');
/* Instantiate the Mxit API */
$key = '50709335604c4feeaf9009b6e5f0424a1';
$secret = '45a7b65b216d4f638a3cf4fc84f4e802f';
$app = 'guniver3se';
$message = $_GET["message"];
$api = new MxitAPI($key, $secret);
$api->get_app_token('message/send');

foreach ($batches as $batch) {
    $list = join(',', $batch);
    // process the batch list here
    $api->send_message($app, $list, $message, 'true');
}

echo 'Success<br>';


?>
</body>
</html>
Link to comment
Share on other sites

I assume HTTP_X_MXIT_USERID_R contains the users username? If so then try doing  a simple str_replace

$raw = file_get_contents($source) or die("Cannot read file");
$raw = str_replace("$mxituid\n", "", $raw);
file_put_contents($source, $raw);

I see no need for regex for removing a user from the file.

Link to comment
Share on other sites

I assume HTTP_X_MXIT_USERID_R contains the users username? If so then try doing  a simple str_replace

$raw = file_get_contents($source) or die("Cannot read file");
$raw = str_replace("$mxituid\n", "", $raw);
file_put_contents($source, $raw);

I see no need for regex for removing a user from the file.

Yes it recall the username - and still the username is found inside the unique.csv file. Selecting the option to remove it have no effect. What I'm basicly trying to achieve is to give users the opportunity to add and remove their usernames from my unique.csv Because part 4 of my script will be using the unique.csv file to keep sending pop up messages to the people on the unique.csv file. So I'm not sure what I'm doing wrong in my script because the broadcasting option aint working neither.

Edited by cobusbo
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.