Second Page Session Variable Not Showing Session Variable From First Page - php

So, I created a session_start and created two html input text boxes on the first page. I thought I correctly coded the if statements in the primary PHP block but alas, am unable to use the submitted values on the secondary page. I've made small alternations on both pages, nothing major, but cannot get the second page to show these session variables. What am I missing here?
//FIRST Page
<!DOCTYPE>
<?php
session_start();
?>
<html>
<head>
<title>Product Page</title>
<meta charset="UTF-8"
</head>
<body>
<form method=“post” action="orderPage.php">
<p>Enter the number of items you would like to order in each respective text box</p>
<label>Apples <input name="Apples" /></label>
<br>
<label>Bananas <input name=“Bananas” /></label>
<input type="submit" value="Checkout"></form>
<?php
if (isset($_POST['submit'])) {
if (empty($_POST['Apples'])) {
$Apples = 0;
$_SESSION['Apples'] = $Apples;
} else {
$Apples = $_POST['Apples'];
$_SESSION['Apples'] = $Apples;
}
if (empty($_POST['Bananas'])) {
$Bananas = 0;
$_SESSION['Bananas'] = $Bananas;
} else {
$Bananas = $_POST['Bananas'];
$_SESSION['Bananas'] = $Bananas;
}?>
</body>
</html>
//Second Page
<!DOCTYPE>
<?php
session_start();?>
<html>
<head>
<title>Product Page</title>
<meta charset="UTF-8"
</head> <body> <h3>Order Confirmation Page</h3>
<?php
echo "Apples : " . $_SESSION['Apples'] . "<br>";echo "Bananas : " . $_SESSION['Bananas'] . "<br>";?>
<input type="submit" method="post" value="Checkout">
<?php
if (isset($_POST['submit'])){echo "Your order has been placed.";session_destroy();}?>
</body>
</html>

You Have some mistakes, there are, } missing end of the page 1 submit form, and and not using PHP function top of the page. So I have tested and following code will helps you.
page 1
session_start();
$Apples = 0;
$Bananas = 0;
if (isset($_POST)) {
if (empty($_POST['Apples'])) {
$_SESSION['Apples'] = $Apples;
} else {
$Apples = $_POST['Apples'];
$_SESSION['Apples'] = $Apples;
}
if (empty($_POST['Bananas'])) {
$_SESSION['Bananas'] = $Bananas;
} else {
$Bananas = $_POST['Bananas'];
$_SESSION['Bananas'] = $Bananas;
}
//echo json_encode($_SESSION);
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="orderPage.php" method="post">
Apples: <input type="text" name="Apples"><br>
Bananas: <input type="text" name="Bananas"><br>
<input type="submit">
</form>
</body>
</html>
And Your 2nd page
<?php
session_start();
if (isset($_POST)) {
session_destroy();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Product Page</title>
<meta charset="UTF-8">
</head> <body> <h3>Order Confirmation Page</h3>
<?php
echo "Apples : " . $_SESSION['Apples'] . "<br>";echo "Bananas : " . $_SESSION['Bananas'] . "<br>";?>
<form class="" action="" method="post">
<input type="submit" value="Checkout">
</form>
</body>
</html>
Think this will help you.

Related

How do i echo multiple lines of input with this code?

I want the user to enter an animal and it go to the line below their previous input.
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<input type="text" name="animal"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php
function display() {
$animal = $_POST['animal'];
echo "$animal <br>";
}
if(isset($_POST['animal'])){
display();
}
?>
</body>
</html>
It currently only displays 1 line at a time and refreshes the line with new input.
<?php
session_start();
if (!isset($_SESSION['animals']) && $_POST['animal']) {
$_SESSION['animals'] = $_POST['animal'].'<br>';
} elseif (isset($_SESSION['animals']) && $_POST['animal']) {
$_SESSION['animals'] .= $_POST['animal'].'<br>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<input type="text" name="animal"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php if (!empty($_SESSION['animals']) { echo $_SESSION['animals']; } ?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<?php if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo '<input type="hidden" name="animal[]" value="'.$animal.'" >';
}
} ?>
<input type="text" name="animal[]"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php
function display() {
$animals = $_POST['animal'];
foreach($animals as $animal){
echo "$animal <br>";
}
}
if(isset($_POST['animal'])){
display();
}
?>
</body>
</html>

Header redirection on the same page

I have small problem with part of my script:
<?php
session_start();
include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) {
?>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<h1>Markup 1</h1>
</body>
</html>
<?php
} else {
if(isset($_POST['email'], $_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$query = $db->prepare("SELECT * FROM user WHERE user_email = ?");
$query->bind_param('s',$email);
$query->execute();
$query->bind_result($user_id,$user_name,$user_email,$user_password);
$query->fetch();
$user = array("user_id"=>$user_id, "user_name"=>$user_name, "user_email"=>$user_email, "user_password"=>$user_password);
if($user['user_id'] != 0) {
$_SESSION['logged_in'] = true;
header("Location: index.php");
die();
} else {
$error = "Incorrect details!";
}
}
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<h1>Markup 2</h1>
<div class="container">
<h3>Please login</h3>
<?php if(isset($error)) { ?>
<h4><?php echo $error; ?></h4>
<?php } ?>
<form action="index.php" method="post" autocomplete="off">
<input type="text" name="email" placeholder="E-mail">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
Problem is that script after refreshing (calling header() method) doesn't execute die() statement, and after successfully set session variable and rendering part with "Markup 1" it will also render "Markup 2" part but it shouldn't.
I found this example here: https://www.youtube.com/watch?v=UNTvU--o2q8.
You can try including the second markup section within the else block this is a fairly hackish fix, but it should accomplish what you are aiming for. I would recommend restructuring this section and pulling some of the markup out to separate included files.
<?php
session_start();
include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) {
?>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<h1>Markup 1</h1>
</body>
</html>
<?php
} else {
if(isset($_POST['email'], $_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$query = $db->prepare("SELECT * FROM user WHERE user_email = ?");
$query->bind_param('s',$email);
$query->execute();
$query->bind_result($user_id,$user_name,$user_email,$user_password);
$query->fetch();
$user = array("user_id"=>$user_id, "user_name"=>$user_name, "user_email"=>$user_email, "user_password"=>$user_password);
if($user['user_id'] != 0) {
$_SESSION['logged_in'] = true;
header("Location: index.php");
die();
} else {
$error = "Incorrect details!";
}
} ?>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<h1>Markup 2</h1>
<div class="container">
<h3>Please login</h3>
<?php if(isset($error)) { ?>
<h4>
<?php echo $error; ?>
</h4>
<?php } ?>
<form action="index.php" method="post" autocomplete="off">
<input type="text" name="email" placeholder="E-mail">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
<?php } ?>
You can't call header() after you write content to the browser. You can sort of hack around this in PHP using output buffers (it's been a long time), but really you should move code that handles headers above all of your markup.
See: http://php.net/manual/en/function.header.php

Higher / lower game

I'm trying to make a higher / lower number game. So basically you get one number and the start and then you click a button - higher or lower. So if you click higher and the next number is higher you win otherwise you lose , same goes for lower.The problem comes when I click some of the buttons it gives a new number. So how can I prevent this? Here's my code:
<!DOCTYPE html>
<html>
<head>
<title> Gamble </title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include('config.php');
if(!$_SESSION['username']) {
header("Location: index.php");
}
Menu();
isOnline();
$number = rand(0, 100);
echo $number;
if(isset($_POST['higher']) || isset($_POST['lower'])) {
$num = $number;
$newNumber = rand(0, 100);
if($_POST['higher']) {
if($newNumber > $num) {
echo 'You win!<br>'.$newNumber;
}
}
}
?>
<form method="POST">
<input type="Submit" name="higher" value="Higher">
<input type="Submit" name="lower" value="Lower">
</form>
</body>
</html>
*Note: I know the lower button does not work , I just want to make it for higher 'cause it will work on the same way with lower..
EDIT:
Working version:
<!DOCTYPE html>
<html>
<head>
<title> Gamble </title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include('config.php');
if(!$_SESSION['username']) {
header("Location: index.php");
}
Menu();
isOnline();
$num1 = rand(1, 100);
if(isset($_POST)) {
if($_POST['higher']) {
$num2 = rand(1, 100);
$oldNum = intval($_POST['num1']);
if($num2 > $oldNum) {
echo "You win! Your number:".$oldNum." , and the new number is: ".$num2;
}
}
}
?>
<form method="POST">
<input type="Hidden" value="<?= $num1 ?>" name="num1"><?= $num1 ?><br>
<input type="Submit" name="higher" value="Higher">
<input type="Submit" name="lower" value="Lower">
</form>
</body>
</html>

Check to see if PHP session exists not working

I am trying to find a way to check whether there is a session. And if there isn't, redirect back to the start.php page.
These are the pages I have made.
start.php
<form action="form.php" method="post">
<p>Please enter your name to continue:</p>
<input type="text" name="name" id="name" />
<input type="submit" name="enter" id="enter" value="Enter" />
</form>
form.php
Above the head:
<?php session_start(); ?>
In the body:
<?php
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
}
}
$name = $_SESSION['name'];
echo $name;
?>
Attempt
I have putting this above the head of the form (along with what is there now) but it just keeps me on the start.php page
<?php
if (!isset($_SESSION["name"]))
{
header("location: start.php");
}
else{
}
?>
more info
So currently if there is no session and I enter form.php it will redirect me to start.php. But if there is a session it will stay on form.php.
But if I start on start.php and submit the form (creating the session and moving to form.php) it will straight away redirect me back to start.php (the same page)?
code of the two pages in full:
start.php
<?php session_start(); ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
</head>
<body>
<form action="deletethis.php" method="post">
<p>Please enter your name to continue:</p>
<input type="text" name="name" id="name" />
<input type="submit" name="enter" id="enter" value="Enter" />
</form>
</body>
</html>
form.php
<?php
session_start();
if (!isset($_SESSION["name"]))
{
//header("location: delete1.php");
die('<meta http-equiv="refresh" content="0;URL='.'delete1.php'.'" />');
}
else{
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
}
}
$name = $_SESSION['name'];
echo $name;
?>
</body>
</html>
What I have decide to do instead
if (strlen($name) <1){
echo '<script> window.location.replace("delete1.php");</script>';
}
You need to add
<?php session_start(); ?>
on each page in the very beginning..
So, the code should be like this
<?php
session_start();
if (!isset($_SESSION["name"]))
{
//header("location: delete1.php");
die('<meta http-equiv="refresh" content="0;URL=\'delete1.php\'" />');
}
else{
}
?>

Cannot get session value with php

i'm sending session values from my first.php and trying to get from my second.php. I have done some reading about this, and works fine on my localhost, but on my server doesn't work at all.
Here is the code from my first.php file:
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
<title></title>
</head>
<body align="center">
<div id="login">
<?php
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
mysql_connect("localhost", "shopping_katalog", "logik#112233") or die(mysql_error());
mysql_select_db("shopping_katalog") or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT password,id FROM x9qg6_users
where username='" . $username . "'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$userhash = md5($password . $test[1]);
if ($test[0] === $userhash) {
$_SESSION['login_user'] = $user_id;
$_SESSION['username'] = $username;
$url = "biblioteka.php";
header("Location: $url");
}
} else {
echo 'Внесете ги вашите податоци во полињата!';
}
?>
<form action="" method="POST" accept-charset="UTF-8">
Корисничко име:<br/>
<input name="username" id="username" type="text"/><br/>
Лозинка:<br/>
<input type="password" id="password" name="password"/><br/>
<input type="submit" value="Логирај се!"/>
</form>
</div>
</body>
</html>
And here is my second.php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
<title>Библиотека на</title>
</head>
<body>
<div id="wrapper">
<?php
$user_ID = $_SESSION['login_user'];
$logged_user = $_SESSION['username'];
?>
<h1 align="center" id="b_welcome">Добредојде <?php echo $logged_user; ?> во твојата библиотека! </h1>
<h4 align="center" id="b_info">Во твојата библиотека ги имаш следниве книги</h4>
<div id="knigi">
<?php
/* OVA E QUERITO
*
SELECT *
FROM Knigi k, poracki p
WHERE k.knigaid = p.kniga
AND p.korisnikInt = $user_ID
*
*/
//-----------------------------------
if(isset($_SESSION['login_user'])){
mysql_connect("localhost", "user", "pass) or die(mysql_error());
mysql_select_db("shopping_katalog") or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT * FROM Knigi k, poracki p
WHERE k.knigaid = p.kniga AND p.korisnikInt ='" . $user_ID . "'");
while ($row = mysql_fetch_array($result)) {
//knigaid,naslov,avtor,link_do_pdf,thumb_link,kategorija,cena,br_strani
echo '<div id="item">';
echo '<h5 align="center" id="b_item_title">' . $row['naslov'] . '</h5>';
//echo '' . $row['avtor'] . '';
echo '<img src="http://' . $row['thumb_link'] . '" id="b_item_slika" />';
echo '</div>';
}
if (!$result) {
echo 'Проблем со добивање на податоците: ' . mysql_error();
exit;
}
}else{
$url = "/index.php";
header("Location: $url");
}
?>
</div><!--kraj na knigite-->
</div>
</body>
</html>
Here is the concept:
in you first_page.php the user should enter his username and password and if you find them both are correct and match those exist in the database, then you will set a session and store whatever data you want in it, then you will redirect the user to the second_page.php which will use those session stored values to do whatever you want with them. And if the user didn't enter his username and password he will stay in the first_page.php
And here is a simple example from which you can take the main concept and apply it on your case:
in first_page.php
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body align="center">
<div>
<?php
if (!empty($_POST["username"]) && !empty($_POST["password"])) {
$_SESSION['username'] = $_POST["username"];
header("Location: second_page.php");
}
?>
<form action="first_page.php" method="post" accept-charset="UTF-8">
Username: <input type="text" name="username" /><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
And in second_page.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h3>Welcome <?php echo $_SESSION["username"] ?></h3>
</body>
</html>
To check if cookies are enabled or not, use the code...
<?php
setcookie('test', 1, time()+3600);
if(!isset($_GET['cookies'])){
header('Location:/info.php?cookies=true');
}
if(count($_COOKIE) > 0){
echo "Cookies are Enabled!";
} else {
echo "Disabled";
}
?>

Categories