Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to do a simple thing, create a login page that shows successful login and logoff messages, but it always shows the successful login message ("Logado com sucesso"). My hosting is Hostinger. Here is my code:
<?php
require_once('db.php');
session_start();
if(isset($_POST['action'])){
if($_POST['action'] = 'login'){
$_SESSION['login'] = $_POST['login'];
$msg = 'Logado com sucesso!';
} elseif($_POST['action'] = 'logoff') {
$msg = 'Deslogado com sucesso!';
unset($_SESSION['login']);
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<div id="container">
<form method="POST">
<? if(!$_SESSION['login']){ ?>
<h3>Preencha os seus dados abaixo</h3>
<input type="hidden" name="action" value="login">
<input type="text" required name="login" placeholder="Nome de usuário">
<input type="password" required name="senha" placeholder="Senha">
<input type="submit" value="Entrar">
<? } else { ?>
<h3>Você está logado como</h3>
<input type="hidden" name="action" value="logoff">
<input type="text" required name="login" disabled value="<?=$_SESSION['login']?>">
<input type="submit" value="Sair">
<? } ?>
<? if(isset($msg)){ ?>
<div id="msg"><? echo $msg?></div>
<?} ?>
</form>
</div>
</body>
</html>
Your if statements are performing assignment and not a comparison.
For example
if($_POST['action'] = 'login')
is assigning $_POST['action'] = 'login' and will always be TRUE.
You need == so that would become
if($_POST['action'] == 'login')
Related
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']);
}
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have an HTML form with three inputs: Make, Year, Mileage.
The conditions are that Make should not be empty, and Year and Mileage must be numeric.
The form submits when the conditions are met.
However, when one of the conditions is not met, I'm using PHP to redirect to the same page and display an error message on the screen. Now when I enter correct values, the form wouldn't submit. Nor does the cancel button redirects it to the previous page.
If I want the form to submit after the error message has been displayed, I have to refresh it and then enter correct values. How can I avoid the refresh?
Here's the code:
<?php
session_start();
require_once "pdo.php";
if (!isset($_SESSION['email']) || strlen($_SESSION['email']) < 1) {
die('Not logged in');
}
if (isset($_POST['cancel'])) {
header("Location: view.php");
return;
}
if (isset($_POST['make']) && isset($_POST['year']) && isset($_POST['mileage'])) {
if (strlen($_POST['make']) < 1) {
$_SESSION['failure'] = "Make is required";
header("Location: add.php");
return;
}
else {
if (!is_numeric($_POST['year']) || !is_numeric($_POST['mileage'])) {
$_SESSION['failure'] = "Mileage and year must be numeric";
header("Location: add.php");
return;
}
else {
$sql = "INSERT INTO autos(make, year, mileage)
VALUES(:mk, :yr, :ml)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':mk' => $_POST['make'],
':yr' => $_POST['year'],
':ml' => $_POST['mileage']
));
$_SESSION["record"] = "Record inserted";
header("Location: view.php");
return;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mohammed Misran's Automobile Tracker</title>
</head>
<body>
<h1>Tracking Autos for <?php echo htmlentities($_SESSION['email']);?></h1>
<?php
if (isset($_SESSION['failure'])) {
echo '<p style="color: red;">'.htmlentities($_SESSION['failure'])."</p\n";
unset($_SESSION['failure']);
}
?>
<form method="post">
<p>Make:
<input type="text" name="make" size="40"/></p>
<p>Year:
<input type="text" name="year" size="40"/></p>
<p>Mileage:
<input type="text" name="mileage" size="40"/></p>
<p><input type="submit" value="Add"/>
<input type="submit" name="cancel" value="Cancel"/></p>
</form>
</body>
</html>
Good Afternoon,
I think its better to use HTML form properties, you want to validate the entered values, so its easy to do it like this:
1- 'Make' should not be empty:
<input type="text" name="make" size="40" required/>
2- 'Year' and 'Mileage' must be numeric:
<input type="number" name="year" size="40"/>
<input type="number" name="mileage" size="40"/>
by this way you dont need to write any php validation for your form, if you want to use php validation let me know to guide you for solving your problem
when Submit button press then first you have to Check Submit Button Post like this:
if(isset($_POST['submit'])){
//Your Code Logic...
}
then get data from POST & store it in variable like this... for Your Reference
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
in html...
<body>
<form method="post" action="">
<input type="text" name="username" placeholder="enter username"><br/><br/>
<input type="text" name="password" placeholder="enter password"><br/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
then store in database like this
$sql="insert into user(username, password)
values('$username','$password')";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
<?php
session_start();
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer">
<form action="second.php" class="formLayout" method="post">
<div class="formGroup">
<label>Name:</label>
<input type="text" name="first_name"><br>
<input type="hidden" name="postback" value="true"><br>
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" value="Mustang">Ford Mustang<br>
<input type="radio" name="model" value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" value="Corvette">Corvette<br>
</div>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION["first_name"] = $_POST["first_name"];
$_SESSION["model"] = $_POST["model"];
}
?>
</div>
</body>
</html>
I set up my code to set the value of the "first_name" and "model" names into my session variables.
When I try to access the values of the stored variables in the submission page of the form:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION["first_name"];
echo $_SESSION["model"];
?>
</body>
</html>
I only receive the out of the model value, not the first_name value. I don't understand what I'm doing wrong here.
Let's assume your first file is called first.php. As shown in your <form>, the second one is second.php.
It seems that you are writing the data into the session in the first.php file, but this code will never run because you are submitting your form to second.php and not first.php!
So, move the code that writes the session variables into second.php instead. To test that it really worked, you can create a third.php to display them.
(I'm not sure why see the model set, but I guess it's still there from a previous test you did.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
session_start() is called in connect.php but is not included here for security reasons
I am having trouble with a session not being unset or destroyed
I have a logout.php where i handle the logging out and it looks like this
<?
if(isset($_POST['logout'])){
session_start();
session_unset();
session_destroy();
header("Location: index.php");
}
?>
I then call this code in my navigation which is working fine however once i log in and try to log out the navigation should go away and the forms should be displayed but the navigation is the only thing being display here is that script
<nav>
<?
if(isset($_SESSION['user'])){
include("server/constants/nav.php");
} else{?>
<div class="form">
<input type="button" id="displayLoginForm" onclick="showLogin()" value="Login">
<input type="button" id="displayRegisterForm" onclick="showRegister()" value="Register">
<div id="loginSection" style="display:none">
<? include("server/display/LoginForm.php") ?>
</div>
<div id="registerSection" style="display:none">
<? include("server/display/RegisterForm.php") ?>
</div>
</div>
<? } ?>
</nav>
Other relevant scripts
HandleLogin.php
<?
if(isset($_POST['Login'])){
$user = $_POST['username'];
$pass = $_POST['password'];
$sql = "SELECT * FROM energywise WHERE username='$user'";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$firstName = htmlspecialchars($row->firstname);
$lastName = htmlspecialchars($row->lastname);
$username = htmlspecialchars($row->username);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->email);
if(empty($id)){
echo 'Account does not exist';
} elseif($pass != $password){
echo 'Passwords do not match';
} else{
$_SESSION['user'] = $id;
header("Location: index.php");
}
}
?>
nav.php
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/navigation.css">
<script src="js/nav.js"></script>
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.php'>Home</a></li>
<li><a href='calender.php'>Calender</a></li>
<li><a href='locations.php'>Locations</a></li>
<li><a href='#'>Placeholder</a></li>
<form method="post">
<li><input type="submit" name="logout" value="Logout"></li>
</form>
</ul>
Well, I don't know, but did you test if logout.php is being accessed? I miss action="logout.php" on the form:
<form action="logout.php" method="post">
<li><input type="submit" name="logout" value="Logout"></li>
</form>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a log in page that I want to pass a value to from another page... but it wont pass can anyone help me? please.
login page code:
<!DOCTYPE html>
<?php
ob_start();
session_start();
include('include/connect.php');
?>
<html>
<head>
<title>test</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" name="login" id="send" />
</form>
</body>
</html>
<?php
// Inialize session
if(isset($_POST['login'])){
$username=$_POST['username'];
$password=$_POST['password'];
$repcode=$_POST['repcode'];
// Include database connection settings
include('connection.php');
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'");
// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
$_SESSION['repcode'] = $_POST['repcode'];
// Jump to secured page
$stat = "UPDATE users SET status='login'";
mysql_query($stat);
header('Location: home2.php');
}else {
}
}
?>
page to be passed:
<!DOCTYPE html>
<?php
ob_start();
session_start();
include('include/connect.php');
?>
<html>
<head>
<title>test</title>
</head>
<body>
<form method="get">
<input type="text" name="username" value="<?php echo $_POST['username']; ?> "/>
<input type="text" name="repcode" value="<?php echo $_POST['repcode']; ?> "/>
</form>
</body>
</html>
I want to pass the username and repcode to another page and put it in textboxes. Can anyone help me with this...I'm new in php and still learning.
I don't understand the goal of your code, but you need to use $_SESSION instead of $_POST in the 2nd page if you want to use the values stored in SESSION:
<form method="get" >
<input type="text" name="username" value="<?php echo $_SESSION['username']; ?> "/>
<input type="text" name="repcode" value="<?php echo $_SESSION['repcode']; ?> "/>
</form>