Cookie lost after another post submit - php

I have filters using $_POST and trying to store them into cookies.
It saves and remembers, but after another $_POST submit cookie is lost.
And yes I have in header.php file session_start() function.
Code:
setcookie('pP', $_POST['perPage'], time()+3600);
if(isset($_COOKIE["pP"]) && $_COOKIE["pP"]==25) {
$per_page=25;
} elseif(isset($_COOKIE["pP"]) && $_COOKIE["pP"]==50) {
$per_page=50;
} elseif(!isset($_COOKIE["pP"])) {
$per_page=25;
}
I've tried using $_SESSION variables too, but still nothing.
Code:
$_SESSION['pP']=$_POST['pocetZaznamu'];
if(isset($_SESSION['pP']) && $_SESSION['pP']==25) {
$per_page=25;
} elseif(isset($_SESSION['pP']) && $_SESSION['pP']==50) {
$per_page=50;
} elseif(!isset($_SESSION['pP'])) {
$per_page=25;
}
How can I fix this problem? I want use $_POST not $_GET and remember $_POST values.

Related

user-defined function for unsetting session is not working

I want a simple function where give a session it just echo it and then unset it. I have written something i have written this:
function sess_caller($sess){
if(isset($sess)){
echo $sess;
}
unset($sess);
}
It does echo the session but doesn't unset it afterwards.
function killSession($key="")
{
if($key!="" && isset($_SESSION[$key]))
{
unset($_SESSION[$key]); //this will empty the key value pair from the
$_SESSION (global) variable in php.
}
else{
//if not needed session anymore just destroy it.
session_destroy();
}
}

two if statements in php

I have two seperate if statements, the first if statement is not working but the second one is.
The first if statement works on my other pages and I am unsure of how to properly code this as I am a beginner to PHP.
<?php
session_start();
if($_SESSION['loggedin'] != 'true') {
header("location:login.php");
}
if ($_SESSION['admin']=='N') {
header("location:errorpage.php");
}
?>
What is true in your conditions? It can be bool type or string type.
If You set like this:
$_SESSION['loggedin'] = TRUE;
$_SESSION['loggedin'] = 'true';
You have got two different variable sets.
You can compare it using == or === to include variable type.
For example:
$_SESSION['test_1'] = TRUE;
$_SESSION['test_2'] = 'true';
var_dump( $_SESSION );
array(2) { ["test_1"]=> bool(true) ["test_2"]=> string(4) "true" }
$_SESSION['loggedin']?
Why don't just clear every SESSION var on logout and if the SESSION vars are set => the user is logged in.
And use after the header(); an exit();
Try var_dump($_SESSION['loggedin']) and edit your question.
Or maybe your loggedin var is not a string but a boolean so you could do if(!$_SESSION['loggedin'])
Try using Boolean values rather than strings. I would also use a const for the admin variables. I would do the following;
$_SESSION['loggedin'] = true/false;
$_SESSION['admin'] = true/false;
public class Priviledges
{
public CONST Admin = 0;
public CONST User = 1;
public CONST Contributor = 3;
//change this to however you want to do it :)
public static function isAdmin($val)
{
if ($val == Priviledges::Admin)
{
return true;
}
else
{
return false;
}
}
}
then when you set the admin session variable you can go;
$_SESSION['admin'] = Priviledges::Admin;
if(!$_SESSION['loggedin'])
{
header("location:login.php");
exit()
}
else if (!Priviledges::isAdmin($_SESSION['admin']))
{
header("location:errorpage.php");
exit()
}
else
{ //do your stuff if none of these conditions are met.. }
Always add an exit() or die() after sending a "Location" HTTP header:
<?php
session_start();
if($_SESSION['loggedin'] !== 'true') {
header("location:login.php");
exit();
}
if ($_SESSION['admin'] === 'N') {
header("location:errorpage.php");
exit();
}
Check: php - Should I call exit() after calling Location: header?.
From aaronsaray blog:
Remember, just because the browser is smart enough not to show the
content, doesn’t mean that this isn’t dangerous. So, it’s a little
less dangerous say if this page is just showing a user search option
or some information. It is much more dangerous if this is a page that
executes an action. This is because the entire PHP page will execute
if you don’t put a die() statement.
On other cases, if you want a condition to be evaluated only when a previous condition is false, you may use a "else if".

Previously set array variable using $_POST method is lost after checking button clicked using $_POST

please forgive my confusing title, here is my problem:
set an array variable through $_POST
check which button has been clicked and process the $_POST variable
What is in my code:
<?php
...
$user = array_filter(array_map('array_filter', $_POST['user']));
...
$submit = isset($_POST['button']) ? trim($_POST['button']) : '';
if ($submit == 'Confirm') {
...do something with $user;
} else if ($submit == 'Cancel') {
...do something else with $user;
}
?>
It appears that when the page is first loaded, $user has been set correctly, however, once the "Confirm" button is clicked, the $name array is lost and cannot be processed. Any idea of how to resolve this will be much appreciated! Many thanks.
an Idea, you need to store it in session. it seems you get $_POST['user'] from previous page/request. variable $_POST only used for passing variable between page, It will not exist anymore if you reload the page.
do it like:
$user = $_SESSION['user'] = array_filter(array_map('array_filter', $_POST['user']));

Determine if $_SESSION superglobal exists in PHP

I want to do something like this:
if ($_SESSION['errors'] exists)
{
//Do stuff
}
I want to create a session on page1 and then go to page2 where it will check for errors, if there are errors it returns to page1 with the errors.
But page1 will give errors if the variable hasn't been created yet on page 2.
If I do $_SESSION['errors'] == "" on page1 it will reset the variable so that's no good.
if (isset($_SESSION['errors']))
{
//Do stuff
}
use isset() and empty() php function.
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
// ...
}
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
// do stuff
}

Check that no variables have been passed

I have one file index.php, I am trying to include
splash.php when no variable is passed
and
display.php when any variable is passed.
This is what i have so far but i want to make it universal for all variables instead of just "query".
if (!isset($_REQUEST['query']))
{
include("splash.php");
}
else {
include("display.php");
}
if (count($_REQUEST) == 0) {
include("splash.php");
} else {
include("display.php");
}
though you're better checking $_POST or $_GET (as appropriate) rather than the looser $_REQUEST

Categories