Jump to content

Recommended Posts

Don't really mind the subject, i didn't know what to label it as but I am fiddling with php and for some odd reason I have tried this about 6 different ways and the coding below is the most accurate way I could think of checking a file & writing the new username to the file..

 

<?php
$open = fopen("users.txt", "r") or die("Error, recheck script.");
$usernames = $_GET['user'];

$users = explode("\n", $open);

foreach($users as $user) {
if (!$user == $usernames) {
$useropen = fopen("users.txt", "a") or die("Error, recheck script.");
$newuser = $usernames . "\n";
fwrite($useropen, $newuser);
fclose($useropen);
echo "New user added";
}
}
fclose($open);
?>

 

Now the problem is no matter what way I try it will not write the URL requested ?user=??? to the file period. I can even take it outside the foreach part and it won't write the name. And as you see I have it set to write if $usernames is not equal to $user (the new lines of users.txt) and it still doesn't write flat out period. I'm dead tired and don't see wtf is up with it. I got it to write a name ONCE and then deleted the .txt and re-uploaded it and re-chmodded it to 0777 and still nada. Idk wtf is up..

 

Any clues?

Like this?

 

$username = $_GET['user'];

$users = file('users.txt');

if (!in_array($username, $users)) {
$fp = fopen('users.txt', 'a');
fwrite($fp, $username . PHP_EOL);
fclose($fp);
}

 

You might want to do some validation on the username though. Otherwise you could insert literally anything into the file.

Ehh sort of, trying to get it to check users.txt and see if any names match the name in $_GET['user'], if it does match then it won't write the name, but if it doesn't match it will write the new name from user to users.txt... That alone adds the user but it doesn't add that check part in that I been seeking originally.

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.