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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

 

 

 

Link to comment
Share on other sites

<?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
?>

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.