How to add multiple passwords to this PHP code? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I don't know PHP and have downloaded this code that works perfectly fine.
I add this code on top of any *.php file and it makes that page, password protected that only opens up if you type the password which in this case is 1234.
Now, I want to add 3 passwords instead of 1. I just don't know where to edit this code.
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
if($pass=="1234")
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>
<?php
if($_SESSION['password']=="1234")
{
?>
<form method="post" action="" id="logout_form">
<input type="submit" name="page_logout" value="Logout">
</form>
<?php
}
else
{
?>
<form method="post" action="">
<div>Protected Content</div>
<br />
<input type="password" name="pass" placeholder="Type your password">
<input type="submit" name="submit_pass" value="Login">
<br /><br />
<div><?php echo $error;?></div>
</form>
<?php
}
?>
Where should I, add what, to be able to have 3 possible passwords?

For this, you may edit password checking line like this:
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
$available_passwords = ['12345','pass123','myOtherPassword'];
if(in_array($pass,$ava_passwords))
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>

Related

How to control the execution of PHP code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
with this example I recap my problem:
The first time I execute this php page I want to echo "I have two buttons".When the user click buttons "next" or "next1" I want to echo "I already said I have two buttons" and not "I have to buttons" again.I proved also with hidden input text but it doesn't work. thk for help
<?php
if ($_SESSION['already_said'] == false ){
echo "I have two buttons". date('h:i:s')."<br>";
}
$_SESSION['already_said']=true;
if( isset($_GET["next"]) || isset($_GET["next1"])) {
echo "I already said I have two buttons". date('h:i:s')."<br>";
}
?>
<html>
<body>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>
Of course the important thing is to remember to put the session_start() at the top of any script that uses the session. In fact it is a good idea to put it in all your scripts even if a few dont use the session just to keep the session alive.
<?php
session_start();
$msg = "I have two buttons ". date('h:i:s')."<br>";
if (! isset($_SESSION['already_said']) ){
$_SESSION['already_said'] = true;
}
if( (isset($_GET["next"]) || isset($_GET["next1"]) ) && isset($_SESSION['already_said'])) {
$msg = "I already said " . $msg;
}
?>
<html>
<body>
<div> <?php echo $msg; ?></div>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>

can i show what i entered input into sessions? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i want display all with session what i enter into input.. but only one shows .. i want display past results too with session.. here my codes;
<form method="post">
isim gir
<input type="text" name="isim" id="isim[]" />
<input type="submit" name="gir" value="sözler" />
</form>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_POST["gir"])){
$isim=$_POST["isim"];
echo $_SESSION["name"]=$isim;
}
?>
Create a search_history session variable that contains an array of all the searches.
if (!isset($_SESSION['search_history'])) {
$_SESSION['search_history'] = array();
}
if (isset($_POST['gir'])) {
$isim = $_POST['isim'];
$_SESSION['search_history'][] = $isim;
}
session_start() should be placed first thing in your code, and if you want to print everything in the array of $_SESSION you can do:
echo "<pre>",print_r($_SESSION),"</pre>";
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$sanitize_post = filter_input_array(INPUT_POST);
if(!empty($sanitize_post)){
$_SESSION['input'][] = $sanitize_post;
}
function reducer($acc, $val)
{
return $acc .= $val;
}
function allUserInput()
{
# Extract user input from session.
if(empty($_SESSION)) return;
foreach ($_SESSION['input'] as $key) {
echo "<li>".array_reduce($key, 'reducer', '')."</li>";
}
}
?>
<form method="POST">
<ul><?php echo allUserInput(); ?></ul>
<input type="text" name="isim" id="isim" />
<input type="submit" name="gir" value="sözler" />
</form>

Error in login logout system [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
When I am submitting the form then I am landing on login.php and getting so many errors.
I have put errors on following link:
https://docs.google.com/document/d/1AqwZ71iAQgZGMU-W0Go6czbpgGenF_f7ucgmEFw0AHo/edit
I am newbie and struggling for last 5 hours with above code.
I have following pages in my login logout system:
db.php
<?php
$conn=new mysqli('localhost','root','','people');
?>
index.php
<?php
session_start();
?>
<html>
<head></head>
<body>
<form method="post" name="login" action="login.php">
<label for="name" class="labelname"> Username </label>
<input type="text" name="username" id="userid" required="required" /><br />
<label for="name" class="labelname"> Password </label>
<input type="password" name="password" id="passid" required="required" /><br />
<input type="submit" name="submit" id="submit" value="Login" />
</form>
</body>
</html>
login.php
<?php
include('db.php');
session_start();
{
$user=mysqli_real_escape_string($_POST['username']);
$pass=mysqli_real_escape_string($_POST['password']);
$fetch=mysqli_query("SELECT id FROM `user` WHERE
username='$user' and password='$pass'");
$count=mysqli_num_rows($fetch);
if($count!="")
{
session_register("sessionusername");
$_SESSION['login_username']=$user;
header("Location:profile.php");
}
else
{
header('Location:index.php');
}}
?>
logout.php
<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
session.php
<?php
include('db.php');
session_start();
$check=$_SESSION['login_username'];
$session=mysqli_query("SELECT username FROM `user` WHERE username='$check' ");
$row=mysqli_fetch_array($session);
$login_session=$row['username'];
if(!isset($login_session))
{
header("Location:index.php");
}
?>
profile.php
<?php
include('db.php');
session_start();
$check=$_SESSION['login_username'];
$session=mysqli_query("SELECT username FROM `user` WHERE username='$check' ");
$row=mysqli_fetch_array($session);
$login_session=$row['username'];
if(!isset($login_session))
{
header("Location:index.php");
}
?>
I think there is something wrong with your db.php file.
You are only supposed to pass 3 parameters,viz, servername, username and password, whereas I see you are passing 4, one being blank.
Please check if
<?php
$conn=new mysqli('localhost','root','people');
?>
or
<?php
$conn=new mysqli('localhost','','');
?>
works.

Incorrect username / password message and redirect [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Creating a Client Login area. I have my log in form in a clientLogin.html:
<form name="ClientLogin" method="post" action="login.php"> <br>
<p>
<label for='Username'>Username</label> <br>
<input type="text" name="username" />
</p>
<p>
<label for='Password'>Password</label> <br>
<input type="password" name="password" />
</p>
<br>
<input type="submit" value="Submit" />
</form>
If the username & password combination are incorrect I want to redirect to this page adding a message saying "incorrect username/ password please try again". This is the code from login.php:
<?php
//Connect to the database
require('db.php');
$user = mysql_real_escape_string($_POST["username"]);
$pass = mysql_real_escape_string($_POST["password"]);
$clientdata = mysql_query("SELECT * FROM Users WHERE username='$user' and password='$pass'")
or die (mysql_error());
$data = mysql_fetch_array($clientdata, MYSQL_ASSOC);
if(mysql_num_rows($clientdata) == 1){
session_start();
$_SESSION['username'] = $user;
header('Location: profile.php');
}else{
header('Location: clientLogin.html');
}
I expect the clientLogin.html files needs changing to a .php file. other than that I am stumped. Any guidance would be greatly appreciated.
You can set a SESSION flag and when calling ClientLogin.
$_SESSION['loginerror'] = 1;
in ClientLogin.php
if ($_SESSION['loginerror'] > 0) {
/* Display Appropriate Error message */
}
You need to change extension of clientLogin.html from .html to .php
Change header('Location: clientLogin.html'); to header('Location: clientLogin.php');
Add this part of code at the top of your clientLogin.php page:
<?php
session_start();
if(empty($_SESSION['username'])) {
echo 'incorrect username/ password please try again.' ;
}
?>
   
you should use exit(); after redirecting to other file like..
header('Location: profile.php');
exit();
you please use mysqli instead of mysql .its not going to be supported by
new version of php.

How Do I give a navigation link in php while using forms [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If the login code is validated, I should go into the member page otherwise I should say on the same page..I am not sure how to write a navigation link to another page..I have seen couple of answers using headers but I didn't get it.
login.php
if($username==$dbusername&&$password==$dbpassword)
{
// If this condition is true I should go into member page
}
else
{
echo "incorrect password!"; //should stay in the same page
}
form action= "member.php" method="post"
Username: input type="text" name="username"<br/>
Password: input type="password" name="password"<br/>
input type="submit" value="LogIn"><br/><br/>
Simply use header like this:
if($username==$dbusername&&$password==$dbpassword)
{
header("location:member.php");
}
If you want a delay in the redirect you can use this:
header("Refresh: 5;url=klanten.php");
(this will wait 5 seconds before redirecting)
What you want to do can be achieved by posting to the same page, login.php
Before any html, check:
if (isset($_POST['username'])) {
if($username==$dbusername && $password==$dbpassword) {
header("location: member.php");
}
}
header("location: member.php") simply redirects to member.php if condition true.
So the form would look something like this:
form action="login.php" method="post">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" value="LogIn" />
</form>

Categories