Multiple isset checks php - php

I'm trying to implement multiple if(isset()) statements but I can't get it to work.
example:
if (isset($_GET['a']) or isset($_GET['b'])) {
// HTML
} else {
// HTML
<a href="?link=a>LinkA</a>
<a href="?link=b>LinkB</a>
}
When I click a or b I still got the else statement executed.
I also tried:
if (isset($_GET['a'] or $_GET['b']))
but then I get a error
I'm trying to display different pages on different $_GET requests.
Can someone point me in the right direction or is this not the right way to do this?

Change if (isset($_GET['a']) or isset($_GET['b'])) To:
if ( (isset($_GET['link']) && $_GET['link'] == 'a') OR (isset($_GET['link']) && $_GET['link'] == 'b']) )

You are checking wrong variable.
You need to check $_GET ['link']

Related

how to get a post or get variable

I have an html form that depending on the checkbox my java-script change it from GET to POST. This piece of code is currently working.
My question is I have a variable on the server side that I need to get. The html variable is sent either as POST or GET, but not sure how to retrieve that variable regardless or what method the html uses. I know how to get the variable as either POST or GET manually, but Not sure how to go about accomplishing this automatically. Any suggestions?
$myVariable = $_GET['htmlVariable'] or $myVariable = $_POST['htmlVariable']
1) Use $_GET if you know that data is coming via URL parameter
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
2) Use $_POST if you know that data is coming via HTTP POST method
if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
3) If you don't know use $_REQUEST
if (isset($_REQUEST['htmlVariable'] && $_REQUEST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
It sounds like you're looking to check if one of the two methods is found. If it is, use that method, and if not, use the other method. To achieve this you can use isset(), and note that you'll also want to check that the string isn't empty:
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$myVariable = $_GET['htmlVariable'];
}
else if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$myVariable = $_POST['htmlVariable'];
}
else {
$myVariable = 'something else';
}
Also note that the order in which you check for the methods may make a difference, depending on your logic.

PHP OR statement not working

I am trying to create a function that checks part of the URL on a page and then either shows or hides a nav bar based on the results. I can get the function to work if it checks for only one string but adding different strings in an OR statement doesn't do anything.
Below is an example of the function working with a single condition...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
This correctly prevents the navbar from showing up on the "login" page. But if I also want the navbar to not show up on the "register" page and do something like this...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false || strpos($url,'/register') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
...the functionality stops working entirely and the navbar shows up on all pages.
So how can I refine my OR statement so that it works properly? Thanks!
Logic flaw. Change the or condition to and
if(strpos($url,'/login')==false || strpos($url,'/register')==false)
Should be
if(strpos($url,'/login')===false && strpos($url,'/register')===false)
You want a && ... not a ||. Because you want the $url to not contain "/login" AND "/register". Otherwise it'll always be true.
try with () like this:
if ( (strpos($url,'/login') === false) || (strpos($url,'/register') === false) ){ ?>

Is it possible to override a page with another include?

THE PROBLEM
The problem I've having is I can't override the current page with include.
if ($LS::getUser('clan') || isset($_GET['clan']) && !isset($_GET['search'])) {
include("res/templates/clan-overview.php");
} else {
include("res/templates/clan-search.php");
}
I don't get why I can't change the page with clan-search.php because I said that if $_GET['search'] is NOT set execute the else statement. But for some weird reason I'm still getting true even though I said that if the user has a CLAN || $_GET['clan'] AND $_GET['search'].
What I've Tried
That code I put at the top is actually the modified version of my original code which I thought would fix the problem. If you are interested in looking at the original code which I don't think will help you here it is:
if ($LS::getUser('clan') || isset($_GET['clan'])) {
include("res/templates/clan-overview.php");
} else if (isset($_GET['search'])) {
include("res/templates/clan-search.php");
}
#Drew pierce gave you first response as you need to wrap the OR parts as a group. try this:
if (($LS::getUser('clan') || isset($_GET['clan'])) && !isset($_GET['search'])) {

PHP - Creating Multiple Conditional Statements For a Single Element

I'm still new in PHP and I need to know how I can create two or more conditional statements for a single elements. For instance, maybe if I had a form that I wanted to perform a certain action if some conditions were true.
I understand that one of the way is to group the statements like:
if(arg1 && arg2 && arg3) {
echo "All of these statements are true";
}
How can I separate this condition but making sure that it executes at the same time?
You can use nested if statements to accomplish the same check.
if(arg1)
{
if(arg2)
{
if(arg3)
{
echo "All of these statements are true";
}
}
}
Try this.
if(($something == 1 ) && ($someotherthing !=7) && ($something == 2)) {
echo "All of these statements are true";
}
Changed answer after what I understood of you clarification:
If you want to know if the posted form has all the required fields in PHP then you could write :
if(isset($_POST['field_name']) && $_POST['field_name'] != "")
{
//The field is set and not empty.
}
else
{
echo "Field not set...";
}
w3schools tutorial on forms :
http://www.w3schools.com/php/php_forms.asp
As for the numeric part of your question take a look at regex :
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
EDIT 2 : As someone mentioned if you want to know if the field is set BEFORE being sent to the server, you will need to use Javascript.

Hiding a div upon login with php

I am trying to hide a div once a user has logged into there account of my online store (built with cartweaver 4 php) but i just cant seem to get it to work.
the php code for defining wether the user is logged in or not is as followed:
if(!strlen($_SESSION["cwclient"]["cwCustomerID"]) ||
$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
$_SESSION["cwclient"]["cwCustomerID"] === "0" ||
$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
and the div i would like to hide upon login, which contains a table, have been given an id and class of:
<div id="newcustomertable" class='newcustomer'>
I have tried applying this method using css: Show Hide div if, if statement is true
but that ended up giving me an undefined variable error on my testing server.
I admit i am a bit of a php newbie, so if anyone who is able to make more sense of this could help out and possibly find a solution it would be much appreciated.
Thank you.
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
Make sure that the condition check is AFTER the div element getting rendered. Because if you put it before the element the page would not have fully rendered and so the script will not be able to get the div that you want to hide.
REVERSE Condition based on OP's request. I know this is a kluge, folks. Dont flame me!
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
//do nothing
}
else
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
You've got at least 3 variables there that could be throwing your undefined variable warning:
$_SESSION["cwclient"]
$_SESSION["cwclient"]["cwCustomerID"]
$_SESSION["cwclient"]["cwCustomerType"]
you need to isset() each of those before doing any tests.
For starters, the strlen function in PHP returns an INT, not a boolean.
Change from:
(!strlen($_SESSION["cwclient"]["cwCustomerID"])
to:
(strlen($_SESSION["cwclient"]["cwCustomerID"]) > 0)
First of all could that not just be simplified to the following if statement to make it easier
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
#code here
}
Second of all to hide the div if they are logged in you could do it like this
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
$shouldhide="style='visibility:hidden;'";
} else {
$shouldhide="";
}
echo"<div id='hidethis' $shouldhide>";
Although if the content is a security risk it'd be better to not output it at all as it's still visible in the source code.

Categories