Higher / lower game - php

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>

Related

How to check a number is armstrong or not using php?

I made a program to check whether a number is a amstrong number or not in php. But it is not working. Cloud anyone tell me what is wrong in this code:
<!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.0">
<title>Document</title>
</head>
<body>
<form action="New Text Document.php" method="post">
<input type="number" placeholder="number" name="amst">
<input type="submit">
</form>
<?php
if(empty($_POST)){
exit;
}
# storing the input in a variable called amst
$amst = $_POST['amst'];
# storing the length of the input in a variable called len
$len = strlen($amst);
$add = 0;
while($amst<=0){
$div = $amst%10;
$pow = pow($div,$len);
$add+=$pow;
$amst = intval($amst/10);
}
#printing the add.
echo $add;
?>
</body>
</html>
Here I printed add just to see whether it is working properly or not. Here no matter what the input is the answer is 0. I am only getting 0. Could anyone tell me why I am getting the output as 0. I have implemented amstrong number program in other languages. This is the method I use. But here it is not working.
Your loop requires a negative number. It works only if number <0, try :
$add = 0;
while($amst>0){
$div = $amst%10;
$pow = pow($div,$len);
$add+=$pow;
$amst = intval($amst/10);
}
#printing the add.
echo $add;
if ($add=== $_POST['amst']) {
echo ‘This is an amstrong number’;
} else {
echo ‘Nope’;
}
You can check the number is Armstrong not using blow code
<!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.0">
<title>Document</title>
</head>
<body>
<form action="" method="post">
<input type="number" placeholder="number" name="amst">
<input type="submit">
</form>
<?php
if(empty($_POST)){
exit;
}
$num = $originalNum = $remainder = $result = 0;
# storing the input in a variable called amst
$num = (int)$_POST['amst'];
$originalNum = $num;
for($i=1;$i<=strlen($originalNum);$i++){
// remainder contains the last digit
$remainder = (int)$originalNum % 10;
//store sum of each number's cube
$result += (int)($remainder * $remainder * $remainder);
// removing last digit from the orignal number
$originalNum /= 10;
}
if ($result == $num):
echo $num." is an Armstrong number.";
else:
echo $num." is not an Armstrong number.";
endif;
?>
</body>
</html>

Second Page Session Variable Not Showing Session Variable From First Page

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.

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>

PHP Sessions Dies on next page

I have a login page wich sends a variable to another page verifying if login is true (1) or false (0). I can echo $_SESSION['logon'] on page1 but just as I go to page2, my session seems to be dead and gives me * Notice: Undefined index: logon in * I have already fiddled with php.ini but to no avail. Register_globals = off by the way. I'm a rookie so I might have passed something wrong, didn't change the right parameter in php.ini, don't know.
Login Page:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<title>Alpha</title>
<link rel="stylesheet" href="CSS.css">
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/jquery.history.js"></script>
</head>
<body>
<form id="logbox" method="post" action="verylog.php">
User <input id="user" name="user" type="text" required/>
Password <input id="pass" name="pass" type="PASSWORD" required/>
<input id="send" type="submit"/>
</form>
<?php
if(isset($_POST['user'])){
header("Location: verylog.php");
}
?>
</body>
</html>
verylog.PHP "page1"
<?php
session_start();
$user=$_POST['user'];
$pass=$_POST['pass'];
$_SESSION['logon']=0;
$cost = ['cost' => 10,];
$hasheduser=password_hash($user, PASSWORD_BCRYPT,$cost);
$hashedpass=password_hash($pass, PASSWORD_BCRYPT,$cost);
$storeuserhash=file_get_contents('sunburn/userburn.txt');
$storepasshash=file_get_contents('sunburn/passburn.txt');
if(password_verify($user, $storeuserhash) && password_verify($pass, $storepasshash)){
session_regenerate_id(true);
$_SESSION['logon']=1;
header ("Location: ../sunburn/selector.php");
die();
//echo $_SESSION['logon'];
} else{
header ("Location: ../burnlogin.php");
die();
}
?>
selector.php "Page2"
<?php
session_start();
$logon=$_SESSION['logon'];
if($logon==0){
//header("Location: index.php");
exit();
echo $_SESSION['logon'];
}else{
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<link rel="stylesheet" href="localhost/CSS.css">
<link rel="stylesheet" href="CSS.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="divreg">
<div>
<a href="../sunburn/register.php">
<button type="button" >-REGISTRAR-</button>
</a>
</div>
<div>
<a href="../sunburn/deltePAGE.php">
<button class="button1" type="button" value="deltePAGE" >-DELETAR-</button>
</a>
</div>
</div>
<div id="select">
</div>
</body>
<?php
}
?>
And I'm sorry about any typo I may have sent.
You are using exit before echo and you need to check condition on pag2.php like this :
if(isset($_SESSION['logon']) && $_SESSION['logon'] != "") {
$logon=$_SESSION['logon'];
if($logon==0){
//header("Location: index.php");
echo $_SESSION['logon'];
exit();
}
}

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

Categories