Jump to content

Web page auth with a call to another php script


dave101

Recommended Posts

<?php

 

// Define your username and password

$username = "Fileuser";

$password = "test98";

 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

 

?>

 

<h1>Login</h1>

 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <p><label for="txtUsername">Username:</label>

        <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

 

            <p><label for="txtpassword">Password:</label>

                <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

 

                    <p><input type="submit" name="Submit" value="Login" /></p>

 

                    </form>

 

                    <?php

 

                    }

                    else {

 

                    ?>

 

                    <?

 

 

                    }

 

                    ?>

<?

 

How can i make thsi call another php file after it has authenticated the user? Thanks!

 

Dave

 

 

Something like this:

 

<?php

session_start();

// Define your username and password
$username = "Fileuser";
$password = "test98";

if (!isset($_SESSION['loggedIn'])) {
    $_SESSION['loggedIn'] = false;
}


if (isset($_POST['txtUsername']) && isset($_POST['txtPassword'])) {
    if ($_POST['txtUsername'] === $username && $_POST['txtPassword'] === $password) {
        $_SESSION['loggedIn'] = true;
    }
}


if ($_SESSION['loggedIn']) {

// list your files

}
else {

// display the login form

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
        <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

            <p><label for="txtpassword">Password:</label>
                <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

                    <p><input type="submit" name="Submit" value="Login" /></p>

                    </form>
<?php
} // end display the login form

Thank you for the help. It seems to bypass the login piece of the script and still display the files with no auth. Also is there a way to only list a certain type of fiels in the directory? Such as only list the files that ahve a pdf extention? This is the script I have so far.

 

<?php

 

session_start();

 

// Define your username and password

$username = "Fileuser";

$password = "test98";

 

if (!isset($_SESSION['loggedIn'])) {

    $_SESSION['loggedIn'] = false;

    }

 

 

    if (isset($_POST['txtUsername']) && isset($_POST['txtPassword'])) {

        if ($_POST['txtUsername'] === $username && $_POST['txtPassword'] === $password) {

                $_SESSION['loggedIn'] = true;

                    }

                    }

 

 

                    if ($_SESSION['loggedIn']) {

 

                    // list your files

                    $dir = ".";

 

                    $dh = opendir($dir);

 

                    while (($file = readdir($dh)) !== false) {

                            echo "<A HREF=\"$file\">$file</A><BR>\n";

                            }

 

                            closedir($dh);

 

                    //end list files

 

                    }

                    else {

 

                    // display the login form

 

                    ?>

 

                    <h1>Login</h1>

 

                    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

                    <p><label for="txtUsername">Username:</label>

                    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

 

                    <p><label for="txtpassword">Password:</label>

                    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

 

                    <p><input type="submit" name="Submit" value="Login" /></p>

 

                    </form>

                    <?php

                    } // end display the login form

 

Thanks!

 

 

 

 

<?php

session_start();

// Define your username and password
$username = "Fileuser";
$password = "test98";
if (isset($_POST['txtUsername']) && isset($_POST['txtPassword'])) {
if ($_POST['txtUsername'] == $username && $_POST['txtPassword'] == $password) {
	$_SESSION['loggedIn'] = true;
}
}


if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) {

// list your files
$dir = ".";

$dh = opendir($dir);

while (($file = readdir($dh)) !== false) {
	if (strstr($file, ".pdf")){
		echo "<A HREF=\"$file\">$file</A><BR>\n";
	}
}
closedir($dh);

//end list files

}
else {

// display the login form

                    ?>

                    <h1>Login</h1>

                    <form name="form" method="post" action="?">
                    <p><label for="txtUsername">Username:</label>
                    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

                    <p><label for="txtpassword">Password:</label>
                    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

                    <p><input type="submit" name="Submit" value="Login" /></p>

                    </form>
                    <?php
} // end display the login form
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.