index.php
PHP Code:
<?php session_start( );
/** no point in a username and pass, make an unimaginable string **/
$secret = "thelongstring";
/** going to change these settings in settings.php **/
$change = array(
"root",
"variable"
);
/** need these for foreach( ) **/
include("settings.php");
/** logout a user by request **/
if( $_GET['do'] == 'logout' )
{
session_destroy( );
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
/** if the form was submitted **/
if( $_POST )
{
/** Insure we've posted the right form !! **/
if( $_SESSION['auth'] and !$_POST['secret'] )
{
/** build new php **/
$php[ ] = "<?php";
foreach( $_POST as $key => $value ) $php[ ] = sprintf( "\$%s = \"%s\";", $key, $value );
$php[ ] = "?>";
/** open file handle to settings.php **/
if( ( $settings = fopen( 'settings.php', 'w' ) ) )
{
/** write new code to settings.php **/
if( !fwrite( $settings, implode( "\n", $php ) ) )
{
$msg = "Cannot save settings";
}
fclose( $settings );
}
else $msg = "Cannot open settings.php for writing";
}
/** Login posted **/
elseif( $_POST['secret'] )
{
/** Authorize **/
if( $_POST['secret'] == $secret )
{
$_SESSION['auth'] = true ;
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
else $msg = "Incorrect Secret";
}
else
{
/** Nothing **/
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
}
/** output red message **/
if( $msg ) printf( "<font color=red>%s</font>\n", $msg );
/** include settings again incase of changes **/
include("settings.php");
/** Display setting form if authed **/
if( $_SESSION['auth'] ):
?>
<form action="" method="POST">
<?php
foreach( $change as $key )
{
printf( "<p><label>%s <input type=text name=\"%s\" value=\"%s\"></label></p>\n", $key, $key, $$key );
}
?>
<p><input type="submit" value="Save" /></p>
</form>
<p><a href="?do=logout">Logout</a></p>
<?php
/** and display login form if not **/
else:
?>
<form action="" method="POST">
<p><label><input type="password" name="secret" /></label></p>
<p><input type="submit" value="Authorize"/></p>
</form>
<?php endif; ?>
settings.php needs to be writable by server ( chmod 777 )
PHP Code:
<?php
$root = "root values";
$variable = "variable valus";
?>
I do not suggest that you keep authorization data in plain text, it's a crap idea, I also assume that you're just messing about here.
Passwords should always be hashed or encrypted when you're authorizing users, it's also not a great idea to have settings in a file that anyone on the filesystem can include ( in most environments ).
Have fun ...
Bookmarks