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
}
Related
First ever question on stackoverflow (new user) so bare with me.
I have a website www.website.com/?view=overview .
I want to test if VIEW exists, if it does not then I redirect to another page
(this is done with empty($_GET['view') and works
If that test passes, I now want to get that view is one of 5 values, if it is then I allow the user to continue if not I redirect to another page.
I have tried with a variable and array, with in_array and the most I can get it to do is properly recognize a single value. When I try to test for more than one, it always redirects. Here is the example if I am testing for two values (overview and settings) but again the total number tested should be 5.
if (empty($_GET['view'])) {
header("Location: www.website.com/?view=overview");
} else {
if (($_GET['view'] != "overview") || ($_GET['view'] != "settings")) {
header("Location: www.website.com/?view=overview");
}
}
}
I searched everywhere and I am stuck!
You're using || (OR) when you should be using && (AND)
if (empty($_GET['view'])) {
header("Location: www.website.com/?view=overview");
} else {
if (($_GET['view'] != "overview") && ($_GET['view'] != "settings")) {
header("Location: www.website.com/?view=overview");
}
}
}
If $_GET['view'] equals 'settings' then it's not equal to 'overview', that part of the condition will pass and your code will redirect. And vice versa.
You can use in_array(needle, haystack) having a list of valid values:
$valid_views = ['overview', 'settings'];
if (!in_array($_GET['view'], $valid_views){
header("Location: www.website.com/?view=overview");
} else {
// view is ok, continue
header("Location: www.website.com/?view=".$_GET['view']);
}
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".
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.
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");
}
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