i have this script how to adding some criteria? 1. if password wright then redirect to a.php 2. a.php is same code sent "$Password" value to a.php remark : a.php is same code start.php start.php code <?php $Password = 'demo'; if (isset($_POST['submit_pwd'])){ $pass = isset($_POST['passwd']) ? $_POST['passwd'] : ''; if ($pass != $Password) { showForm("รหัสผ่านผิด"); exit(); } } else { showForm(); exit(); } function showForm($error="LOGIN"){ ?> <?php echo $error; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">Password: <input class="text" name="passwd" type="password"/> <input class="text" type="submit" name="submit_pwd" value="Login"/> </form> <?php } ?> To achieve the desired functionality of redirecting to a.php if the password is correct and passing the $Password value to a.php, you can modify your start.php code as follows: <?php $Password = 'demo'; if (isset($_POST['submit_pwd'])) { $pass = isset($_POST['passwd']) ? $_POST['passwd'] : ''; if ($pass != $Password) { showForm("รหัสผ่านผิด"); exit(); } else { header("Location: a.php?Password=" . urlencode($Password)); exit(); } } else { showForm(); exit(); } function showForm($error = "LOGIN") { ?> <?php echo $error; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">Password: <input class="text" name="passwd" type="password"/> <input class="text" type="submit" name="submit_pwd" value="Login"/> </form> <?php } ?> In this modified code: If the password is correct, it uses the header function to redirect to a.php with the password as a query parameter. The password is passed to a.php using the URL parameter Password. Now, in a.php (or start.php, as you mentioned they are the same), you can retrieve the password using $_GET['Password']. Ensure that you handle the password securely and don't expose sensitive information in the URL parameters.
เพิ่มเงื่อนไข
auto check and submit<?php $PasswordX = 'xxx'; // Replace 'xxx' with the actual password for a.php $PasswordY = 'yyy'; // Replace 'yyy' with the actual password for b.php if (isset($_POST['submit_pwd'])) { $pass = isset($_POST['passwd']) ? $_POST['passwd'] : ''; if ($pass == $PasswordX) { header("Location: a.php?Password=" . urlencode($pass)); exit(); } elseif ($pass == $PasswordY) { header("Location: b.php?Password=" . urlencode($pass)); exit(); } else { showForm("รหัสผ่านผิด"); exit(); } } else { showForm(); exit(); } function showForm($error = "LOGIN") { ?> <?php echo $error; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">Password: <input class="text" name="passwd" type="password"/> <input class="text" type="submit" name="submit_pwd" value="Login"/> </form> <?php } ?>
If you want to automatically check and submit the password on a.php
after being redirected from start.php
, you can use JavaScript. Here’s an example of how you can achieve that:
Modify your a.php
file to include JavaScript code that automatically submits the form on page load:
<?php
$receivedPassword = isset($_GET['Password']) ? urldecode($_GET['Password']) : '';
if ($receivedPassword == 'xxx') {
// Replace 'xxx' with the actual password for a.php
?>
<html>
<head>
<title>a.php</title>
</head>
<body>
<h1>Welcome to a.php</h1>
<form id="passwordForm" action="a.php" method="post">
<input type="hidden" name="passwd" value="<?php echo $receivedPassword; ?>"/>
</form>
<script>
document.getElementById('passwordForm').submit();
</script>
</body>
</html>
<?php
} else {
// Handle the case where the password is not correct
echo "Invalid password for a.php";
}
?>
This code includes a JavaScript script that automatically submits the form with the correct password. Replace 'xxx' with the actual password for a.php.
Please note that automatically submitting forms on page load can have security implications, so make sure you handle sensitive information securely and consider the security implications of your application.