I am working with PHP session variables, but the problem is that the session variables are not carrying over. I checked using Firebug, and found that the session variables are not being created at all.
print_r($_SESSION); returns an empty array after the page returns to index.php, but displays it correctly before the refresh.
logincheck.php:
<?php
session_start();
error_reporting(-1);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
{
$username=$_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Smart Shopping Cart System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<style type="text/css">
.show-grid{
background:#E0E0E0;
}</style>
</head>
<body>
<?php
include "connection.php"
?>
// ** Some Content**
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Log In | SmartCart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-brand">
SmartCart
</div>
</div>
</div>
<div class="container">
<br />
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<form role="form" method="post" name="login" action="test.php">
<div class="form-group">
<label for="username">Operator Username</label>
<input type="username" class="form-control" name="username" placeholder="Enter your username">
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-default" name="sub">Submit</button>
</form>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body></html>
<?php
}
?>
test.php
<?php
session_start();
error_reporting(-1);
?>
<html><head><title>Redirecting...</title>
</head>
<body>
Redirecting....
<?php
include "connection.php";
if(isset($_POST['sub']))
{
$u_name=$_POST['username'];
$password=mysql_escape_string($_POST['pass']);
$password=mysql_escape_string($password);
$w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'";
$b=mysql_query($w) or die(mysql_error()."in query $w");
$num_rows = mysql_num_rows($b);
if($num_rows > 0)
{
$_SESSION['logged_in']=true;
$_SESSION['username']=$_POST['username'];
print_r($_SESSION);
echo '<meta http-equiv="refresh" content="0; url=index.php">';
}
else
{
echo "<script>
alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>";
}
}
?>
</body></html>
Please help.
Spot the difference:
logincheck:
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
^^^^^^^^^
test:
$_SESSION["loggedin"]=true;
^^^^^^^^
One of these things is not like the other...
I think the under score is missing in on test.php page
in $_SESSION['loggedin']=true;
change this to $_SESSION['logged_in']=true;
As you are using this in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
there is no error with you session part the error is may be in you db access part check your database connection. you can check you session by commenting the query part, it is working fine.
Related
So I have been trying so much to fix this code but it's just not working. It first goes to my login page after I click on my blog which is what I want but the problem is that when I enter the login info it just brings me back to the login page as if nothing changed even if the login info is correct.
I am making a login and logout feature to a blog with PHP but without a database. I have 3 files concerning this:
This is my login.php page:
<?php
// (A) LOGIN CHECKS
require "check.php";
// (B) LOGIN PAGE HTML
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Portfolio</title>
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="portfolio_style.css" />
<link rel="stylesheet" href="login_style.css" />
</head>
<body>
<div class="container1">
<header class="header1">
<h1 class="title1">My name</h1>
<p class="subtitle"><i>Hello World!</i></p>
</header>
<section class="section1">
<nav class="nav1">
<ul>
<li>
<span class="nav2"></span>About Me
</li>
<li>
<span class="nav2"></span>Experience
</li>
<li class="active">
<span class="nav2"></span>Blog
</li>
</ul>
</nav>
<div class="container2">
<div class="content">
<?php
if (isset($failed)) { echo "<div>invalid username or password</div>"; }
?>
<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Login</button>
<div id="id01" class="modal">
<form class="modal-content animate" method="post" target="_self">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="container4">
<label for="user"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="user" required>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
</div>
</form>
</div>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</div>
</div>
</section>
</div>
</body>
</html>
this is the file used to check and store the passes and users: check.php
<?php
// (A) START SESSION
session_start();
// (B) HANDLE LOGIN
if (isset($_POST["user"]) && !isset($_SESSION["user"])) {
// (B1) USERS & PASSWORDS - SET YOUR OWN !
$users = [
"abc" => "123",
"def" => "456",
"ghi" => "789"
];
// (B2) CHECK & VERIFY
if (isset($users[$_POST["user"]])) {
if ($users[$_POST["user"]] == $_POST["password"]) {
$_SESSION["user"] = $_POST["user"];
}
}
// (B3) FAILED LOGIN FLAG
if (!isset($_SESSION["user"])) { $failed = true; }
}
// (C) REDIRECT USER TO HOME PAGE IF SIGNED IN
if (isset($_SESSION["user"])) {
header("Location: blog.php"); // SET YOUR OWN HOME PAGE!
exit();
}
and this final one is the landing page for my blog: blog.php
<?php
session_start();
//LOGOUT
if (!isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
//BACK TO LOGIN PAGE IF NOT SIGNED IN
if (!isset($_SESSION["user"]))
{
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Manel's Portfolio</title>
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="portfolio_style.css" />
</head>
<body>
<div class="container1">
<header class="header1">
<h1 class="title1">My name</h1>
<p class="subtitle"><i>Hello World!</i></p>
</header>
<section class="section1">
<div class="container2">
<div class="content">
<h2 class="title2">My Blog</h2>
<p>
</p>
</div>
<!-- LOGOUT -->
<form method="post">
<input type="hidden" name="logout" value="1"/>
<input type="submit" value="logout"/>
</form>
</div>
</section>
</div>
</body>
</html>```
Your are checking not isset, so they always logout.
Change this
if (!isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
to
if (isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
The echo statement is not printing when the answer does not equal to 8.So if the answer is other than 8 it should print in red "Please try again " underneath the textbox.All it shows is nothing when I click the register button.
<?php
session_start();
$nameErr = "";
$answer = $_POST['lastname'];
if ($_POST['reg']){
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<!Doctype html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>
</head>
<html>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname">
<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block" value="Register"
type="submit" name="reg">
You need to put the echo in the <body> of the document. Also, <html> needs to be at the top.
<?php
session_start();
$nameErr = "";
?>
<!Doctype html>
<html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>
</head>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname">
<?php
if (isset($_POST['reg'])){
$answer = $_POST['lastname'];
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block" value="Register" type="submit" name="reg">
</div>
</div>
</form>
</body>
</html>
You print the error span above the actual HTML code. Just move the if statement down. Like that:
...
<div class="row">
<?php
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
?>
</div>
<div class="form-group col-xs-5 col-xs-offset-3.5">
...
I want login/ to be successful
This is my connect File for starting session
<?php
$connect_error='Error...'
session_start();
$con=mysql_connect('localhost','root','')or die(connect_error);
mysql_select_db('lothlorien',$con) or die(connect_error);
$errors=array();
require 'Functions/users.php';
require 'Login/L1.php';
require 'Functions/General.php';
This is My Login Widget File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lothlorien</title>
<!-- CSS -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/form-elements.css">
<link rel="stylesheet" href="assets/css/style.css">
<style>
.row{
margin:auto;
width:700px;
}
</style>
</head>
<body>
<?php
include 'Core/Database/connect.php';
?>
<div class="top-content">
<div class="inner-bg">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2 text">
<h1>Lothlorien Login Form</h1>
</div>
</div>
</div>
<div class="content">
<div class="row">
<div class="form-box">
<div class="form-top">
<div class="form-top-left">
<h3>Login to our site</h3>
<p>Enter username and password to log in:</p>
</div>
<div class="form-top-right">
<i class="fa fa-lock"></i>
</div>
</div>
<div class="form-bottom">
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn">Sign in!</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="assets/js/jquery-1.11.1.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.backstretch.min.js"></script>
<script src="assets/js/scripts.js"></script>
<!--[if lt IE 10]>
<script src="assets/js/placeholder.js"></script>
<![endif]-->
</body>
</html>
?>
this is my Login for login validation
<?php
include 'Core/Database/connect.php';
include 'Core/Functions/Users.php';
if(empty($_POST)===false)
{
$form_username = $_POST['form-username'];
$form_password = $_POST['form-password'];
}
if(user_exists($form_username)===false)
{
$errors[]="Register before Logging in";
}
else if(user_active($form_username)===false)
{
$errors[]="Not activated Your account";
}
This is where i want the validation to happen
else
{
$login=login($form_username,$form_password);
if($login=== false)
{
$errors[]="Wrong Username and password combination";
}
This is where the redirection takes place
else
{
$_SESSION['user_id']=$login;
header('Location:index.php');
exit();
}
}
print_r($errors);
?>
And finally these are my functions...
<?php
function user_exists($form_username)
{
$form_username=sanitize($form_username);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username'");
return (mysql_result($query,0) == 1) ? true :false;
}
function user_active($form_username)
{
$form_username=sanitize($form_username);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username' AND 'active'= 1");
return (mysql_result($query,0) == 1) ? true :false;
}
function user_id_from_username($username)
{
$form_username=sanitize($username);
$query=mysql_query("SELECT 'user_id' FROM 'user' WHERE 'Username'='$form_username' ");
return (mysql_result($query,0,'user_id'));
}
function login($form_username,$form_password)
{
$user_id=user_id_from_username($form_username);
$form_password=md5($form_password);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username' AND 'Password'= '$form_password'");
return (mysql_result($query)==1)? $user_id:false;
}
?>
I don't know the root of my problem so I kinda posted all up
Problems I encounter
1)No error messages showing up..
2)want to redirect to index.php after successful login
3)Successful Message should be displayed once login is done
Database entries are correct.
I cannot seem to link my login page to my home page, any help would be much appreciated. I know its connecting to the database as i can create users but for some reason its not letting me sign in with a created user.
<?php require ("insert.php"); ?>
<?php
if(isset($_POST[ 'Login' ]))
{
$email= mysqli_real_escape_string($con, $_POST ['email']);
$pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);
$result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' ");
$row = $result->fetch_array(MYSQLI_BOTH);
session_start();
$_SESSION["User ID"] = $row['UserID'];
header ('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>AMSadler login</title>
<meta charset="utf-8">
<meta name="description" content="description of webpage">
<meta name="keywords" content="keywords go here">
<meta name="author" content="Anthony">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/login.css">
<link rel="index" href="index.php">
<link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
<div class="header">
<div id="logo">
<img src="img/logo.png" alt="logo" title="AMSadler.com"/>
</div>
<div id="signup">
<button type="button">Sign up</button>
</div>
</div>
<div id="login">
<form>
<input type="text" name="email" placeholder="Email address">
<br>
<input type="password" name="password" placeholder="Password">
<br>
<input id="Login" type="submit" name="Login" value="Login">
</form>
</div>
<footer>
<div id="copyright">
<p>© Copyright 2015</p>
</div>
</footer>
</body>
</html>
You need to change your form tags.
From,
<form>
To,
<form action="yourPageName.php" method="post">
You need to specify your method type so in your PHP code you can get it using that method. If you leave out the method part it by default uses $_GET. Seeing as your code is pointing to $_POST then you set it to method="post".
You also need to set action="" this can be set to # for same page or leave it blank or using a file name. This will redirect the form to that page.
I have made a simple form with a checkbox that echos out true or false depending on if the checkbox is set or not, however every time i click submit once the checkbox control has been ticked it seems to reset.
How can i modify my code to store the value of any form controls so if i tick the box it remains ticked after i have clicked submit?
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<input type="checkbox" name="formCheckbox" value="Yes"/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Add the checked property &
Change the way the check box is displayed:
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<?php
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
echo '<input type="checkbox" name="formCheckbox" value="Yes" checked/>';
}
else
echo '<input type="checkbox" name="formCheckbox" value="Yes"/>';
?>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>