How to remove and change the value inside of Session php? - php

I got 2 pages schedule.php and sched_confirmation.php in the schedule.php what I do there is I have a code that calculate a fee and store it in Session,
$calc=$result+$result1+$result2+$result3+$result4+$result5+$result6+$result7+$result8+$result9;
$num_format=number_format($calc,2);
$_SESSION['fee']= $num_format;
and in the sched_confirmation here is I want to show the value of the session
<form method="post">
<?php echo $_SESSION['fee'];
<button class="btn btn-warning" name="cancel">Cancel</button>
</form>
what I want is if I click the cancel button it would remove the value of session and redirect to schedule.php and create a new value for the session.
here is what I got so far
<?php
session_start();
if (isset($_POST["cancel"])) {
if (isset($_SESSION['fee'])) {
unset($_SESSION['fee']);
}else
{
$_SESSION['fee'];
}
header("Location: schedule.php");
}
?>
it does redirect me to the previous page but when I create new value for the session when its done and go to sched_confirmation.php it shows "Undefined index: fee "
How can I fix this? thank you in advance

From your question I can say you always want $_SESSION['fee'] to be available, So this is what you need
<?php
session_start();
if ( isset($_POST["cancel"]) ) {
if (isset($_SESSION['fee'])) {
$_SESSION['fee'] = 'New Value';
}
header("Location: schedule.php");
}
?>
There is no need to unset $SESSION['fee'] if you want it to be available later, Just update the value.

You need to set session value.
try this
<?php
session_start();
if (isset($_POST['cancel'])) {
if (isset($_SESSION['fee'])) {
unset($_SESSION['fee']);
} else {
$_SESSION["fee"] = "Session value";
}
header("Location: schedule.php");
}
?>

Related

Retrieve session content from DB if session value is not set with PHP

Im generating a menu dinamically from the database using a DB function. This function depends on the user profile. So when a user login, the function will paint the menu content like this:
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
The problem is that each time I access to one of the pages, the php code calls the DB function, so I want to store the menu content on a _SESSION value and use it until session finish.
So, I try this. If $_SESSION['showmenu'] is empty call the DB function showmenuweb() and assign the menu content to $_SESSION['showmenu'] and if its not empty then, just $_SESSION['showmenu']; content.
if(!isset($_SESSION['showmenu'])) {
echo "there is no menu on session";
$_SESSION['showmenu'] = showmenuweb('ADMIN');
} else {
echo "there is menu on session";
echo $_SESSION['showmenu'];
}
Then I call the session content with:
<php
echo $_SESSION['showmenu'];
?>
But this code always call the function showmenuweb(), its looks like always the $_SESSION['showmenu'] is empty.
Any help will be apreciated.
UPDATED: This is the code:
<?php
include_once('init.php');
if(!isset($_SESSION))
{
session_start();
}
echo $_SESSION['showmenu'];
if(!isset($_SESSION['showmenu'])) {
echo "there is no menu on session";
$_SESSION['showmenu'] = showmenuweb('ADMIN');
} else {
echo "there is a menu on session";
}
?>
<!DOCTYPE html>
<html lang="en">
<body>
<?php
echo $_SESSION['showmenu'];
echo '<br />';
echo '<br />';
showmenuweb('ADMIN');
?>
</body>
</html>
The html output shows:
there is no menu on session
menu1
menu2
menu3
menu1
menu2
menu3
I fix the problem by using two session variables.
First assign the db value $_SESSION['MENU'] = $row['SALIDA'];
Then I check it.
if(!isset($_SESSION['MENU'])) {
$_SESSION['SALIDA'] = showmenuweb('USER');
}
Thanks to all for the help.

Why isn't my function returning the value it should? [duplicate]

So I'm new to php/the programming world and I'm studying online and other forms and such but I couldn't find anything to help answer my question which is why I'm here. Any help is certainly appreciated, Thanks!
I want to turn the below code into a function that I can call. It works just as it stands below as in it outputs a 1 if I check my check box in my form and remains 0 if I don't touch my checkbox.
$activeMain = (isset($_POST['activateBox'])) ? $_POST['activateBox'] : false;
if ($activeMain == true) {
$activeMain = '1';
}
However when I try to use a function to do the same thing, and I select my checkbox to display a '1', it remains 0 and if I do a var_dump the output is now "on" instead of 1 like how it is supposed to be.
Below is the function I tried:
function activeCheck($activeMain) {
$activeMain = (isset($_POST['activateBox'])) ? $_POST['activateBox'] : false;
if ($activeMain == true) {
$activeMain = '1';
}
return $activeMain;//I messed around with a return value
and as far as I can tell, it has no effect.
}//ends activeCheck function
activeCheck($activeMain);//call to function
In all I'm confused on why it shows "on" when I try to use a function as well as how to get it to work.
EDIT:
How do I turn my original code (first bit of code posted above) into a function?
What values should I use / can I use something else besides _SESSION to check if user has selected the checkbox from the form?
I have a HTML/PHP form in which I give the option to select a checkbox. If users hit the checkbox, the input they provided will output a '1' for a true value.
My HTML/PHP form:
<?php session_start(); ?>
<!DOCTYPE HTML>
<HTML>
<head>
<title>PHP FORM</title>
</head>
<body>
<form method="post" action="processForm.php">
Name: <input type="text" name="names" required = "required"><br>
<input type="submit" value="Create Users" onclick="formNAMES"><br>
Activate: <input type="checkbox" name="activateBox">
<?php
if (isset($_SESSION ['error'])) {
foreach ($_SESSION['error'] as $value) {
echo $value;
}
session_destroy();
unset($_SESSION['error']);
}
/* Above if statement checks if $_SESSION variable has been set in processForm page. If it has,
an error message corresponding to the error shows up on redirect to this form. The unset makes sure
the $_SESSION is destroyed upon completion of the process. */
?>
</form>
</body>
</html>
Change related line to following: The problem is when activateBox is not empty it assings itself to $activeMain naturally.
$activeMain = isset($_POST['activateBox']);
Updated : Check this one.
function activeCheck() {
return isset($_POST['activateBox']);
}
activeCheck();
Updated Answer Due To Updated Question :
I removed session_destory if you execute that and if you have another session variable ex: user isLoggedIn it would be destroyed too. unset is OK for the purpose. Please check XSS, Sql injection attacks around the internet implement logic according to best practises, and validate/sanitize your data before process parameters into DB or etc.
<?php
// formView.php
ob_start();
session_start();
?>
<!DOCTYPE HTML>
<HTML>
<head>
<title>PHP FORM</title>
</head>
<body>
<form method="POST" action="processForm.php">
Name: <input type="text" name="names" required = "required"><br>
<input type="submit" value="Create Users" onclick="formNAMES"><br>
Activate: <input type="checkbox" name="activateBox">
<?php
if (isset($_SESSION ['error'])) {
foreach ($_SESSION['error'] as $value) {
echo $value;
}
unset($_SESSION['error']);
}
/* Above if statement checks if $_SESSION variable has been set in processForm page. If it has,
an error message corresponding to the error shows up on redirect to this form. The unset makes sure
the $_SESSION is destroyed upon completion of the process. */
?>
</form>
</body>
</html>
<?php ob_end_clean(); ?>
<?php
// processForm.php
if (!empty($_POST)) {
$safeParameters = [];
foreach ($_POST as $key => $val) {
// sanitize your inputs. #see XSS, SQL injection etc.
// validate parameters according to your needs.
$safeParameters[$key] = $val;
}
$_POST = [];
checkIsActivated($safeParameters);
// implement other logic,
// save form to database etc.
}
function checkIsActivated($parameters)
{
return !empty($parameters['activateBox']);
}
?>

Why is my function changing boolean value to 'on'?

So I'm new to php/the programming world and I'm studying online and other forms and such but I couldn't find anything to help answer my question which is why I'm here. Any help is certainly appreciated, Thanks!
I want to turn the below code into a function that I can call. It works just as it stands below as in it outputs a 1 if I check my check box in my form and remains 0 if I don't touch my checkbox.
$activeMain = (isset($_POST['activateBox'])) ? $_POST['activateBox'] : false;
if ($activeMain == true) {
$activeMain = '1';
}
However when I try to use a function to do the same thing, and I select my checkbox to display a '1', it remains 0 and if I do a var_dump the output is now "on" instead of 1 like how it is supposed to be.
Below is the function I tried:
function activeCheck($activeMain) {
$activeMain = (isset($_POST['activateBox'])) ? $_POST['activateBox'] : false;
if ($activeMain == true) {
$activeMain = '1';
}
return $activeMain;//I messed around with a return value
and as far as I can tell, it has no effect.
}//ends activeCheck function
activeCheck($activeMain);//call to function
In all I'm confused on why it shows "on" when I try to use a function as well as how to get it to work.
EDIT:
How do I turn my original code (first bit of code posted above) into a function?
What values should I use / can I use something else besides _SESSION to check if user has selected the checkbox from the form?
I have a HTML/PHP form in which I give the option to select a checkbox. If users hit the checkbox, the input they provided will output a '1' for a true value.
My HTML/PHP form:
<?php session_start(); ?>
<!DOCTYPE HTML>
<HTML>
<head>
<title>PHP FORM</title>
</head>
<body>
<form method="post" action="processForm.php">
Name: <input type="text" name="names" required = "required"><br>
<input type="submit" value="Create Users" onclick="formNAMES"><br>
Activate: <input type="checkbox" name="activateBox">
<?php
if (isset($_SESSION ['error'])) {
foreach ($_SESSION['error'] as $value) {
echo $value;
}
session_destroy();
unset($_SESSION['error']);
}
/* Above if statement checks if $_SESSION variable has been set in processForm page. If it has,
an error message corresponding to the error shows up on redirect to this form. The unset makes sure
the $_SESSION is destroyed upon completion of the process. */
?>
</form>
</body>
</html>
Change related line to following: The problem is when activateBox is not empty it assings itself to $activeMain naturally.
$activeMain = isset($_POST['activateBox']);
Updated : Check this one.
function activeCheck() {
return isset($_POST['activateBox']);
}
activeCheck();
Updated Answer Due To Updated Question :
I removed session_destory if you execute that and if you have another session variable ex: user isLoggedIn it would be destroyed too. unset is OK for the purpose. Please check XSS, Sql injection attacks around the internet implement logic according to best practises, and validate/sanitize your data before process parameters into DB or etc.
<?php
// formView.php
ob_start();
session_start();
?>
<!DOCTYPE HTML>
<HTML>
<head>
<title>PHP FORM</title>
</head>
<body>
<form method="POST" action="processForm.php">
Name: <input type="text" name="names" required = "required"><br>
<input type="submit" value="Create Users" onclick="formNAMES"><br>
Activate: <input type="checkbox" name="activateBox">
<?php
if (isset($_SESSION ['error'])) {
foreach ($_SESSION['error'] as $value) {
echo $value;
}
unset($_SESSION['error']);
}
/* Above if statement checks if $_SESSION variable has been set in processForm page. If it has,
an error message corresponding to the error shows up on redirect to this form. The unset makes sure
the $_SESSION is destroyed upon completion of the process. */
?>
</form>
</body>
</html>
<?php ob_end_clean(); ?>
<?php
// processForm.php
if (!empty($_POST)) {
$safeParameters = [];
foreach ($_POST as $key => $val) {
// sanitize your inputs. #see XSS, SQL injection etc.
// validate parameters according to your needs.
$safeParameters[$key] = $val;
}
$_POST = [];
checkIsActivated($safeParameters);
// implement other logic,
// save form to database etc.
}
function checkIsActivated($parameters)
{
return !empty($parameters['activateBox']);
}
?>

CakePHP Login element

Maybe a simple question but how do I change my login box after logging in. Such as 'Welcome user'
I can't find good examples ...
My code looks like this
users_controller
function login {
}.. with login element
See here as example: http://groups.google.com/group/cake-php/browse_thread/thread/56ff0ce37fb06a30
You have 2 options:
Depending of your login state, choose another element, like:
function login() {
if ($isUserLoggedIn == false) {
// render login element
} else {
// render welcome element
}
}
this is more bad option In login element add logic like:
if (!$isUserLoggedIn) {
// echo html and code for login
} else {
// echo html and code for welcome
}
I suppose that you want an action/view that can be rendered by both logged and not logged in users.
Similar to riky's the following code might help you
In your controller:
//check if user is logged in and set $user_details variable in the view
if($this->Auth->User()){
$user_details = $this->Auth->User();
}
$this->set(compact('user_details'));
In your view:
<? //check if $user_details variable is set (user is logged in) and display the correct element
if(isset($user_details){
echo $this->element('welcome_box',array('user_details'=>$user_details));
}else{
echo $this->element('login_box');
}
?>

PHP function to check if 2 variables are empty

I have an index page, I want it to include a page called splash.php and not display.php when a user lands on index.php, but once a user does something (sets a variable) ie if a user searches (variable "query") i want it to include display.php and not include splash.php
What is wrong with this code?
function hasGet()
{
return !empty($_GET['fact']);
return !empty($_POST['query']);
}
if (hasGet()) {
include("display.php");
}
else {
include("splash.php");
}
This question should be removed
Only the first return statement is executed. Try:
return !empty($_GET['fact']) && !empty($_POST['query']);
A better way to accomplish what you are trying to do is use sessions.
index.php
<?php
session_start();
if (!isset($_SESSION['visited'])) {
$_SESSION['visited'] = true;
include 'splash.php';
} else {
include 'display.php';
}
?>
This way after a user visits index.php for the first time, $_SESSION['visited'] is set to true and it won't show the splash page throughout their visit.
You cannot have two returns as you are doing. Try
return (!empty($_GET['fact']) && !empty($_GET['query']));
You might want to try this...
if($_SERVER["SCRIPT_NAME"] == "/index.php"){
include("splash.php");
}else{
include("display.php");
}
2.
if(!empty($_GET["fact"]) || !empty($_POST["query"])){
include("display.php");
}else{
include("splash.php");
}

Categories