session not being unset or destroyed [closed] - php

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>

Related

Variable use previous value in PHP [closed]

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')

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>

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.

php Session not working in chrome and IE [closed]

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 8 years ago.
Improve this question
I have php session not working in chrome and IE but working fine in Firefox.
I'm getting this error in page4.:
it show variable empty in page4, session not passing in page4 after the user click click me in page3
Notice: Undefined index: username in /var/www/html/phptest/test4.php on line 5
Please see my code and let me know where the error is.
Page2
<html>
<body>
<form action="test3.php" method="post">
Username: <br><input type="text" name="username"></br>
<input type="submit" name = 'submit1' value= 'Login'>
</form>
</body>
</html>
Page3
<html>
<body>
<?php
session_start();
$username = $_POST['username'];
$_SESSION['username']= $_POST['username'];
echo "<br> Hi $username.</br>";
?>
<form action="test4.php" method="post">
<input type="submit" name = 'submit' value= 'click me'>
</form>
</body>
</html>
Page 4
<?php
session_start();
$username = $_SESSION['username'];
echo "<br> Hi $username.</br>";
?>
session_start() must go at the top of the page before any output:
<?php
session_start();
?>
<html>
<body>
<?php
$username = $_POST['username'];
$_SESSION['username']= $_POST['username'];
echo "<br> Hi $username.</br>";
?>

pass value to another page in php [closed]

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>

Categories