Jump to content

Recommended Posts

I am making an file uploader, and I have got everything done except the register form. You see, this hoster, unlike other codes, offers users individual galleries and directories so they can organize their own set of images. However, there are 2 problems I need sorting out. Before we begin, here is the code that processes the registration:

 

<?php

$username = $_POST['username']; 
$password = $_POST['password']; 

$username = strtolower($username);
$password = strtolower($password);

$all_users = file("user.db.txt");
    foreach($all_users as $user_line)
    {
                $user_arr = explode(" | ", $user_line);
        if($user_arr[0] == $username){ echo 'This username has already been taken! <a href="register.php">Try again</a>' }
    }

$File = "user.db.txt";
$Handle = fopen($File, 'a');
$Data = $username;
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = md5($password);
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = "4\n";
fwrite($Handle, $Data);
print "You have registered successfully! click <a href=index.htm>here</a> to return to the login screen";
fclose($Handle);

mkdir("$username", 0700);
?>

 

So what this basically does is convert their username and password to lowercase, write it to user.db.txt, and create them a directory. Now, the first of my two problems:

 

How do you write to a .php file, not a .txt? I've tried the code above and just replacing .txt with .php, but it doesn't seem to work.

 

The second is, the code isn't functioning because of this code:

 

$all_users = file("user.db.txt");
    foreach($all_users as $user_line)
    {
                $user_arr = explode(" | ", $user_line);
        if($user_arr[0] == $username){ echo 'This username has already been taken! <a href="register.php">Try again</a>' }
    }

 

this was my attempt at a code that would stop users from signing up with someone else's username, thus making it possible for them to access the other user's gallery. So my question is simply, what's wrong with it?

 

NOTE: it is meant to explode ' | ' not '|'.

 

Opinions? Tom.

Link to comment
https://forums.phpfreaks.com/topic/67324-solved-2-easyish-questions/
Share on other sites

It appears I owe you an apology. Strange, everything else auto 777'd o_O. Thanks a bunch :)

 

So yeah, question 2.

 

<?php

$username = $_POST['username']; 
$password = $_POST['password']; 

$username = strtolower($username);
$password = strtolower($password);

$all_users = file("users.db.php");
    foreach($all_users as $user_line)
    {
                $user_arr = explode(" | ", $user_line);
        if($user_arr[0] == $username){ echo 'This username has already been taken! <a href="register.php">Try again</a>' }
    }

$File = "users.db.php";
$Handle = fopen($File, 'a');
$Data = $username;
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = md5($password);
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = "4\n";
fwrite($Handle, $Data);
print "You have registered successfully! click <a href=index.htm>here</a> to return to the login screen";
fclose($Handle);

mkdir("$username", 0700);
?>

 

I think the problem is that $user_line is still undefined. But I can't think how I'd define it o_O how do you say 'array[0] on every line' in PHP? :P

A better topic name might help to bring attention to your questions.

 

As for your question 2, try printout out your variables to check their values.  Use var_dump() rather than print if possible, as it is more detailed.

 

In your code there is no need to define $user_line, as it is defined by the foreach loop.

try reading the logic....!

 

Sighs..

 

<?php

$username = $_POST['username']; 
$password = $_POST['password']; 

$username = strtolower($username);
$password = strtolower($password);

$all_users = file("users.db.php");
$freeuser = true;
foreach($all_users as $user_line)
{
$user_arr = explode(" | ", $user_line);
if($user_arr[0] == $username)
{
	echo 'This username has already been taken! <a href="register.php">Try again</a>';
	$freeuser = false;
	break;
}
}
if($freeuser)
{
$File = "users.db.php";
$Handle = fopen($File, 'a');
$Data = $username;
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = md5($password);
fwrite($Handle, $Data);
$Data = ' | ';
fwrite($Handle, $Data);
$Data = "4\n";
fwrite($Handle, $Data);
print "You have registered successfully! click <a href=index.htm>here</a> to return to the login screen";
fclose($Handle);

mkdir("$username", 0700);
}
?>

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.