Jump to content

Creating a PHP Login page (Admin page already exists)


pherank

Recommended Posts

I have a textfile (users.txt) with the following arrays written to it -

 

[Cracker Jack]
clientname=Cracker Jack
password=swordfish

[PanAm]
clientname=PanAm
password=blowfish

 

How do I access this data with PHP? The first question is how to loop through all the data and display it to screen. The second would be, if the user enters a "clientname" and "password" in a form, how to find a match (or no match) in the textfield arrays?

 

Here is some code I'm using to try to loop through the array data - not yet working correctly:

 

<?php
echo "<h1>Manual access to each element from associative array</h1>";

$fp = fopen ( 'users.txt', 'r' );
while ( $line = fgetcsv ($fp, 100, "\t")) {

for ($row = 0; $row < 2; $row++)
{
    echo $line[$row]["clientname"]." password: ".$line[$row]["password"];
    echo "<br />";
    }
}

echo "<h1>Using foreach loop to display elements</h1>";

echo "<ol>";
for ($row = 0; $row < 2; $row++)
{
    echo "<li><b>The row number $row</b>";
    echo "<ul>";

    foreach($line[$row] as $key => $value)
    {
        echo "<li>".$value."</li>";
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>

Link to comment
Share on other sites

This is research for a non-database project. If you can figure out how to create simpler arrays with the admin page script, let us know. Here's the admin page code (I think it was developed by Barry Andrew). I added a couple of things, but have commented out the password encryption for now.

 

<?php
# initialise variables
$clientname = $password = $repassword = "";
$data = array();
$changed = false;

# define your data file and its path, ensure you have write permission to folder
$myTextFile = 'users.txt';

# get the data from current file
    if (file_exists($myTextFile))
        $data = parse_ini_file($myTextFile, true);


# check if user data form submitted
if (isset($_POST['userdata'])) {

if ($_POST['password'] != $_POST['repassword']) {
	echo "<p>Passwords must match</p>";
}
else if (!empty($_POST['clientname']) && !empty($_POST['password']) && !empty($_POST['repassword']) ) {
        # add new data (or update if exists already)
	$salt = time() . rand (90, 4596);
        $data[$_POST['clientname']]['clientname'] = $_POST['clientname'];
        //$data[$_POST['clientname']]['password'] = crypt($_POST['password']) . $salt;
	$data[$_POST['clientname']]['password'] = $_POST['password'];
	// No need to store the retyped password
        //$data[$_POST['clientname']]['repassword'] = $_POST['repassword'];
    
        $changed = true;
}
else echo "<p>Must have Client Name and Password</p>";
}

# do we want to delete a record
if (isset($_GET['delid'])) {

    # remove deleted item from the array
    unset($data[$_GET['delid']]);
    $changed = true;
}

# or do we want to edit a record
if (isset($_GET['changeid'])) {
        # get the items we want to edit so they can appear in the form
        $clientname = $_GET['changeid'];
        $password = $data[$clientname]['password'];
        $repassword = $data[$clientname]['repassword'];
}

if ($changed) {
    # now create new output file, writing in ini file format
    $fp = fopen($myTextFile, 'w');

    ksort($data);
    foreach ($data as $key=>$dataArray) {
             fwrite($fp, "[$key]\n");
             foreach ($dataArray as $k => $v) {
                      fwrite($fp, "$k=$v\n");
             }
             fwrite($fp, "\n");
    }
    fclose($fp);
}

# function to list the current data
function listData($data) {
         if (count($data) > 0) {
             echo "<TABLE border='0' cellspacing='1' cellpadding='2'>\n";
             # headings
             echo "<tr style='background: #C0C0C0'><th>ID</th>";
             list ( $key, $dataArray) = each($data);
             foreach($dataArray as $k=>$v) {
                     echo "<th>$k</th>";
             }
             echo "<th>Del</th></tr>";

             #data
             $i = 0;
             foreach ($data as $key=>$dataArray) {
                     $bg = ++$i % 2 ? "#FFFFFF" : "#EEEEEE";
                     echo "<tr style='background: $bg'>
                            <td>
                            <a href='$_SERVER[php_SELF]?changeid=$key'>$key<a>
                            </td>";
                     foreach ($dataArray as $v) {
                               echo "<td>$v</td>";
                     }
                     echo "<td>[<a href='$_SERVER[php_SELF]?delid=$key'>X<a>]</td></tr>";
            }
            echo "</TABLE>";
        }
        else echo "<p>No data</p>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Expires" CONTENT="Fri, Jan 01 1900 00:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="content-language" content="en">
<META name="generator" content="PHPEd 3.1">
<META name="revisit-after" content="15 days">
<title>User Administration</title>
</head>
<body>
<FORM  method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<INPUT TYPE="HIDDEN"  name="userdata" value="1">
<?php
if (!empty($clientname))
     echo "<INPUT TYPE=\"HIDDEN\"  name=\"clientname\" value=\"$clientname\">";
?>
<TABLE style="background: #E0E0E0; border: solid 2pt #666666" >
<TR>
    <TD colspan='2' style="border-bottom: solid 1pt #666666; font-weight: bold; text-align: center">
    User data
    </TD>
</TR>
<TR>
    <TD>
    Client Name
    </TD>
    <TD>
     <INPUT TYPE="TEXT"  name="clientname" size="30" maxlength="30" value='<?php echo $clientname ?>'<?php echo  empty($clientname) ? '' : ' DISABLED ' ?> >
    </TD>
</TR>
<TR>
    <TD>
    Password
    </TD>
    <TD>
    <INPUT TYPE="TEXT"  name="password" size="30" maxlength="30" value='<?php echo $password ?>'><br>
    </TD>
    </TR>
<TR>
    <TD>
    Retype Password
    </TD>
    <TD>
    <INPUT TYPE="TEXT"  name="repassword" size="30" maxlength="20" value='<?php echo $repassword ?>'><br>
    </TD>
</TR>
<TR>
     <TD>
      
     </TD>
     <TD>
    <INPUT TYPE="SUBMIT"  value="Send data">
    <?php
    if (!empty($clientname))
         echo "<INPUT TYPE=\"BUTTON\"  value=\"Cancel\"   onClick=\"history.go(-1);\">";
    ?>

    </TD>
</TR>
</TABLE>
</FORM>

<?php
     # if we are not editing a record, list the data
     if (!isset($_GET['changeid'])) {
          listData($data);
     }
?>

</body>
</html>

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.