PHP script help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Prozac
Forum Newbie
Posts: 1
Joined: Mon Apr 11, 2005 3:06 pm

PHP script help please

Post by Prozac »

Hi,

Ive been reading alot on forums and tried to create my first PHP script. This script asks the user for an ACCOUNT name and PASSWORD.

It then should create a file with the name of the ACCOUNT (no extension) and put some data in the script.

Somehow ive got problems with my path statement and getting the data the user inputs to work.

Look at the following script:

Code: Select all

<?php
// Get the values from the form
$user = $_GETї'user'];
$pass = $_GETї'pass'];
?>

<table width=100%>
<tr><td width=80%>&nbsp
<td width=20%>
</table>

<?php
$path=&quote;D://textfiles//&quote;;
$filename=$path.$user;

// Check if the file already exists
if (file_exists($filename)) 
{
   // The file already exists
   print &quote;<table><tr><td>Username already exists!</td></tr></table>&quote;;
} 
else 
{
   // The file doesn't exists
   // Create the file

   $newFile=fopen($filename,'a');
   $string=&quote;PASSWORD=$pass
PLEVEL=0&quote;;
   fputs($newFile, $string);
   fclose($newFile);

   echo '<table>
         <tr><td>Account Successfully Created</td></tr>
         </table>';
}
?>
The script gets its data from an index.html :

Code: Select all

<form method=POST action=create.php>
		<input type=hidden name=action value=create>
		<input type=text size=12 name=user>
        <input type=text size=12 name=pass>
        <input name=&quote;submit2&quote; type=submit value=Create>


If i run this script:

1)
If a folder is present on the D drive with the name TEXTFILES it states that account allready exists...

2)
When i delete the folder and try again:

It will create a file named TEXTFILES on D when it should create a textfile D:\Textfile\<account>

3)
The contents of the file is:

PASSWORD=
PLEVEL=0

So the password does not seem to pass from the index.html to the create.php...

What am i doing wrong here?

Below the complete sources from INDEX and CREATE:

Index.html:

Code: Select all

&lt;html&gt;
&lt;title&gt;Account Creation Page&lt;/title&gt;
&lt;body bgcolor=#000000 link=#000000 vlink=#000000 alink=#000000&gt;

&lt;center&gt;
  &lt;p&gt;&lt;img src=&quote;xxx.jpg&quote;&gt;&lt;/p&gt;
  &lt;p&gt;&lt;a href=&quote;stat.xml&quote;&gt;&lt;img src=&quote;online.jpg&quote;&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;p&gt;&amp;nbsp;&lt;/p&gt;
  
&lt;table width=&quote;747&quote; border=&quote;0&quote;&gt;
  &lt;tr&gt;
    &lt;td width=&quote;32&quote; rowspan=&quote;4&quote;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&lt;img src=&quote;login.jpg&quote; width=&quote;140&quote; height=&quote;50&quote;&gt;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width=&quote;163&quote; rowspan=&quote;4&quote;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&lt;img src=&quote;create.jpg&quote; width=&quote;140&quote; height=&quote;50&quote;&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td width=&quote;145&quote; height=&quote;27&quote;&gt;login:&lt;/td&gt;
    &lt;td width=&quote;114&quote; rowspan=&quote;3&quote;&gt;	    
		&lt;form method=POST action=login.html&gt;
	    &lt;input type=text size=12 name=user&gt;
        &lt;input type=password size=12 name=pass&gt;
        &lt;input name=&quote;submit&quote; type=submit value=Login&gt;
        &lt;/form&gt;
	&lt;/td&gt;
    &lt;td width=&quote;140&quote;&gt;login:&lt;/td&gt;
    &lt;td width=&quote;127&quote; rowspan=&quote;3&quote;&gt;		
		&lt;form method=POST action=create.php&gt;
		&lt;input type=hidden name=action value=create&gt;
		&lt;input type=text size=12 name=user&gt;
        &lt;input type=text size=12 name=pass&gt;
        &lt;input name=&quote;submit2&quote; type=submit value=Create&gt;
      	&lt;/form&gt;
	&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;password:&lt;/td&gt;
    &lt;td&gt;password:&lt;/td&gt;
    &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td height=&quote;18&quote;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

Code: Select all

&lt;html&gt;
&lt;title&gt;Account creation&lt;/title&gt;
&lt;body bgcolor=#000000 text=red link=#0000FF vlink=#0000FF alink=#0000FF&gt;

&lt;?php
// Get the values from the form
$user = $_GET&#1111;'user'];
$pass = $_GET&#1111;'pass'];
?&gt;

&lt;table width=100%&gt;
&lt;tr&gt;&lt;td width=80%&gt;&amp;nbsp
&lt;td width=20%&gt;
&lt;/table&gt;

&lt;?php
//change the path to the location for the files!
$path=&quote;D://textfiles//&quote;;
$filename=$path.$user;

// Check if the file already exists
if (file_exists($filename)) 
{
   // The file already exists
   print &quote;&lt;table&gt;&lt;tr&gt;&lt;td&gt;Username already exists!&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quote;;
} 
else 
{
   // The file doesn't exists
   // Create the file

   $newFile=fopen($filename,'a');
   $string=&quote;PASSWORD=$pass
PLEVEL=0&quote;;
   fputs($newFile, $string);
   fclose($newFile);

   echo '&lt;table&gt;
         &lt;tr&gt;&lt;td&gt;Account Successfully Created&lt;/td&gt;&lt;/tr&gt;
         &lt;/table&gt;';
}
?&gt;

echo '&lt;a href=index.html?user=%%user%%&amp;sid=%%sid%%&gt;back to index&lt;/a&gt;';

&lt;/body&gt;
&lt;/html&gt;

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

i took a quick look at your codes:

- you should use $_POST instead of $_GET: that's why your passwrd and user were empty.
- you may want to check the setting of the path to your desired directory
- you also need to check on the security of your script as well.
Post Reply