PHP function to check if 2 variables are empty - php

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");
}

Related

PHP - Test if $_GET value is one of the following

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']);
}

How to use include or require_once on If else statement on php?

I have two file if.php and index.php, I want to use if.php to store the if condition form if statement. But it seems not working. Is it possible to do it? thank you
if.php
<?php
if(Three == 3) {
//do something
}
?>
index.php
<?php
require_once 'if.php';
else{
//do something
}?>
Update:
Beacase I have a lot of index.php(such as index1.php ,index2.php,index3.php.........................index731.php)
If I need to keep update for if statement.....
First day needto add if(One == 1) , Second day need to add if(One == 1, Two== Three)
so If I need to add value in if statement, I need to change a lot of page!!!!!!
but finally ,I find solution.
if.php
<?php
if(Three == 3) {
$session_admin =true;
}
?>
index.php
<?php
require_once 'if.php';
if($session_admin ==true){
//do something
}else{
//do something
}?>
Each PHP file is compiled individually and it must be syntactically correct. It is not possible to start a control structure, function or class in a file and close it in a different file.
It is, however, possible to return a value from an included file. You can use it to implement the behaviour you want like this:
File if.php:
<?php
if (Three == 3) {
// do something
return true;
} else {
return false;
}
File index.php:
<?php
if (require_once 'if.php') {
// do something (or nothing)
} else {
// do something else
}
However, I do not recommend this approach. The best approach is to encapsulate the test in a function:
File if.php:
function testSomething() {
if (Three == 3) {
// do something
return true;
} else {
return false;
}
}
File index.php:
require_once 'if.php';
if (testSomething()) {
// do something
} else {
// do something else
}
Its wrong way to use if else statement , blew code may help you:
index.php
<?php
if($value == 1) {
include_once('first_file.php');
}else{
include_once('second_file.php');
}
// ** OR **
switch($value){
case '1': include_once('first_file.php');break;
case '2': include_once('second_file.php');break;
}
?>
first_file.php
// put here the code that you want run when $value == 1
second_file.php
// put here the code that you want run when $value == 2

No navigation bar displayed after log in

I have 3 different navigation bars, a different 1 has to be loaded depending on the authority of the user. I have made a function in a different php file so i re use the code in each page. My problem is that after logging in , no navigation bar is being loaded. any suggestions to what might be the problem ?
Code in function.php file
function checkAuth()
{
session_start();
if(empty($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
include('regnav.php');
}
else if ($_SESSION['role'] =="admin"){
include('adminnav.php');
}
}
code in the begging of each page
<?php
require_once("function.php");
checkAuth();
?>
Try passing the session result into the function:
place session call at the top of the rendering page
session_start();
$this_session = $_SESSION['role'];
require_once("function.php");
place where you want to render menu
checkAuth($this_session);
the function
function checkAuth($this_session)
{
if($this_session == ''){include('menu.php');}
elseif($this_session == 'registered'){include('regnav.php');}
elseif($this_session == 'admin'){include('adminnav.php');}
}
I would personally restructure your pages just a little, like this:
At the top of each page put:
session_start();
include('function.php');
checkAuth();
Change the function to:
function checkAuth()
{
if (!isset($_SESSION['role'])) { require_once('menu.php'); }
if ($_SESSION['role'] == 'registered') { include('regnav.php'); }
if ($_SESSION['role'] == 'admin') { include('adminnav.php'); }
}
This assumes you aren't using functions in your navbar files, since you don't call any functions to write the navbar. You could, of course, put both navbars into functions and put both functions into a single navbar.php file that you would include at the top of every page (or put the functions into function.php), then call the appropriate navbar function from the checkAuth() function, like this:
if ($_SESSION['role'] == 'registered') { navbar_registered(); }
if ($_SESSION['role'] == 'admin') { navbar_admin(); }

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".

Error on function php

I want go to the home pages as the privilege of user that current logged in.
When i am trying to open the page that having the link it goes to the home page. What changes that i need on my code
<body>
Home
</body
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
header('location:admin_home.php');
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
header('location:home.php');
}
else
{
header('location:user_home.php');
}
}
Your function is being used inside an <a href="...">, so it's clearly supposed to return a URL (also you need to echo it)
Your current code is trying to redirect the user immediately, which won't work because you've already sent <a href=".
Try:
function home() {
if($_SESSION['privillage'] == "ADMIN") return "admin_home.php";
if($_SESSION['privillage'] == "SUPERVISOR") return "home.php";
return "user_home.php";
}
header('location is used if you want a PHP script to redirect the browser to another page. What you are trying to do is simply changing the href based on certain conditions.
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
return 'admin_home.php';
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
return 'home.php';
}
else
{
return 'user_home.php';
}
}
For readability, you could consider using PHP's switch statement
may be you have to capitalize the first letter of "location" use "Location" in header('Location:user_home.php'); :)

Categories