FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


file_exists issue.





cr3ativ3
I have a script here that creates a folder and a file from a register form. I am trying to get it to see if a file_exists first to make sure that someone is not registering over top of another user but the if statement I am using for this is seeming to be completely ignored.

Code:

<?php
// Secure the variables from $_GET attacks.
$username = "";
$password = "";
$email = "";
$template = "";
$file = "";
$usernamefilt1 = "";
$usernamefilt2 = "";
$usernamefilt3 = "";
$passwordfilt1 = "";
$passwordfilt2 = "";
$passwordfilt3 = "";
$passwordfilt4 = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
</head>

<body>
<div align="center">
<form action="register.php" name="registerform" method="post">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="right">Username:</td>
    <td align="left"><input type="text" name="username" /></td>
  </tr>
  <tr>
    <td align="right">Password:</td>
    <td align="left"><input type="password" name="password" /></td>
  </tr>
  <tr>
    <td align="right">Email:</td>
    <td align="left"><input type="text" name="email" /></td>
  </tr>
</table>
<input type="submit" name="submit" value="Register" onclick="confirm('Are you sure you would like to register this user?')" />
</div>
</body>
</html>
<?php
// If the form has been submitted
if (isset($_POST['submit'])) {
// Grab the information from the form, then send it threw some filtering and validation.
$usernamefilt1 = strip_tags($_POST['username']);
$usernamefilt2 = stripslashes($usernamefilt1);
$usernamefilt3 = strtolower($usernamefilt2);
$passwordfilt1 = strip_tags($_POST['password']);
$passwordfilt2 = stripslashes($passwordfilt1);
$passwordfilt3 = strtolower($passwordfilt2);
$passwordfilt4 = sha1($passwordfilt3);

$username = $usernamefilt3 ;
$password = $passwordfilt4 ;
$email = $_POST['email'];

if (file_exists("users/$username/$username")) {
echo "<script language=\"javascript\">alert('This user already exists, please choose a differen't username');</script>";
echo "<meta http-equiv=\"refresh\" content=\"0;url=register.php\">";
exit();
} else {

//Create user directory. Also for linux chmod it to 777.
mkdir("users/$username", 0777);

// Create the structure for the file
$template = stripslashes("<?php \n\$user = \"$username\"; \n\$pass = \"$password\"; \n\$mail = \"$email\"; \n?>");

// Create the user's file
$file = fopen("users/$username/$username.php","w");
// Write the structure of the file to the file
fwrite($file, $template."");
// Close the file
fclose($file);
//Chmod user file to 777 (linux only)
chmod("users/$username/$username.php", 0777);
}
}
?>


I hope you can help, if you need more info just ask.
MrBlueSky
I guess you have to change

Code:

if (file_exists("users/$username/$username")) {


to

Code:

if (file_exists("users/$username/$username.php")) {


Or use

Code:

if (is_dir("users/$username")) {
cr3ativ3
Ya, nothing changes it still seems to be ignoring it.

I would also like to note that I have tested this just by it self in its own file and instead of it displaying the alert and then redirecting I have it so that it tells me if it exists or if it doesn't and it says it does. So, there is nothing wrong with the if script or the server so it must be something to do with it and the rest of that script.

I don't see what the problem is because it seems to be following all syntax rules I am not getting any errors and there are no prominent issues.
cr3ativ3
I don't want to make it look like I'm bumping this thread but I really need to get this solved. (Soon)

So, if anyone has an idea or knows the problem please post thanks.
sonam
PHP sometimes have their flys and in this situations I am not sure what going on.
I don't know what is problem with your script but yesterday I am upload one script and file_exists is working properly. I see only small difference in my code. Variables is not in " (It is too late and I am forget Englis name for " Embarassed Embarassed Embarassed ). Problem also can make $username like file because haven't extension.

My sugestion is chage this line in:

Code:

$userFile = $username . ".php";
if (file_exists("users/" . $username . "/" . $userFile )) {
// ... rest of code


Sonam
cr3ativ3
Ya, still doesn't work hmm... It's not the frihost servers because I have tested it on my local server and it still won't work, thanks though.

Here I have a zip file of the whole system so far if anyone wants to look through everything involved. http://www.oyoystudios.frih.net/sessions.zip

Also, if no one is able to figure it out in the next few days maybe I should report it to php.net as a bug or somethin.

Thanks, Again
sonam
I see one more solution. You allways create new directory and new file with same name for new user. In that case you don't need to check file only if directory exist.

Quote:
Returns TRUE if the file or directory exist
(from PHP.net)


You can try this simple script for testing.

Code:
<?php
$username = "some_existing_directory";
if (file_exists("users/$username")) {
echo "HERE IS!";
exit();
} else {
echo "NOT EXISTS!";
}
?>


BTW your JS alert is not working on my browser!

Sonam
cr3ativ3
Still, won't work hmm.. I wonder if there would be an alternative of doing what I want done. See your script or my script works on its on file but when I add it to this file it doesn't work so I am not sure what is happening it's almost as if its not compatible with other things in this file. Confused
sonam
Maybe you can remove else statment.

Code:
if (file_exists("users/$username/$username")) {
echo "<script language=\"javascript\">alert('This user already exists, please choose a differen't username');</script>";
echo "<meta http-equiv=\"refresh\" content=\"0;url=register.php\">";
exit();
} else {  // this one


and one bracket on the end.

Code:
chmod("users/$username/$username.php", 0777);
}  // this one
}  ?>


If file not exist script will continue without question, if is exist scrip will prompt alert and exit.

Sonam
cr3ativ3
K, I will try it when I get home later from school, I think I have already tried i t and it has no effect but I will try again after school.

Thanks
cr3ativ3
Ok, I just looked back into this and found the problem I can't believe I didn't find it before Embarassed

Anyways here is an image below it is an explanation of what was wrong.


Ok as you can see I have circled three single quotes the javascript alert displays the text inbetween the two single quotes but in the word differen't there is a single quote making it stop them and then the problems occured from there on.

So what I did to fix it was replace the first and last single quotes with double quotes then adds a backslash infront of each because it is a echo "";.

Code:

if (file_exists("users/$username/$username.php")) {
echo "<script language=\"javascript\">alert(\"This user already exists, please choose a differen't username\");</script>";
echo "<meta http-equiv=\"refresh\" content=\"0;url=register.php\">";
exit();
} else {
Related topics
usb external HD
cpanel q: Doesn't seem to be a firewall issue
Building my own comp
Hotmail and Outlook
G3 and Tiger Raid issue, Help
Dungeons and Dragons, in paper issue
PHP VS ASP
PHP issue
The Iran Issue!
Should pilots be able to carry a firearm?
Huge security issue, users can delete other users files,....
Gmail vows!
Web Hosting Issue!?!?
Old fight issue, Flash or Html, Macromedia has a tip?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.