if(!isset($_GET['id']) with to different options with the same variable - php

I have this code: it works fine
<?php
if(!isset($_GET['id']) || $_GET['id'] != '1000'){
header('Location: http://website.com?id=1000');
exit;
}
?>
what i need is to add a second value like this
<?php
if(!isset($_GET['id']) || $_GET['id'] != '1000' or $_GET['id'] !='2000'){
header('Location: http://website.com?id=1000');
exit;
}
?>

Use in_array and add all the values you want to check against into the array:
if(!isset($_GET['id']) || !in_array($_GET['id'], array('1000', '2000'))){
echo 'not 1k or 2 k';
} else {
echo 'is 1k or 2k';
}
Functional example: https://3v4l.org/Q2uao

Divide your condition in to two parts.
First use the !isset condition and then
Put the rest of condition inside if condition
Your Code Look like this :
if(!isset($_GET['id'])){
$id = $_GET['id'];
if($id == 1000 || $id == 2000) {
// Action
}
}

Related

PHP - How to check users level into a login session

I am using this code to protect my pages using login session:
<?php
//PUT THIS HEADER ON TOP OF EACH UNIQUE PAGE
session_start();
if (!isset($_SESSION['username'])) {
return header("location:login/main_login.php");
}
?>
but I'd like to check the users level, because I'd like to show e.g. page-one.php for users level 1 and 2; page-two.php for users level 3 and so on...
I tried in this way but it's not working:
<?php
session_start();
$level = 2;
$_SESSION['level'] = $level;
if (!isset($_SESSION['username'])) {
if($_SESSION['level'] == '0' && $_SESSION['level'] == '1') {
return header("location: page-one.php");
} else if ($_SESSION['level'] == '2') {
return header("location: page-two.php");
}
}
Any advice, please?
EDIT I edited my code, I found another solution: I stored level in a variable and I checked it together the username
This will always be false:
if($_SESSION['level'] == '0' && $_SESSION['level'] == '1')
The same variable can't equal two different values at the same time. Did you mean to use the comparison "or" operator (||) instead?:
if($_SESSION['level'] == '0' || $_SESSION['level'] == '1')
Or perhaps more else-if chaining?:
if($_SESSION['level'] == '0') {
//...
} else if ($_SESSION['level'] == '1') {
//...
} else if ($_SESSION['level'] == '2') {
//...
}

how to tell $_GET parameters are equal to or not equal to

I'm trying to make a dynamic page with $_GET vars(params) and I have it working if the var is equal to something, it will display content. But if the var doesn't equal something, then it either displays the content still, or doesn't display the error; or displays the error and the content at the same time
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
if($type != 'login' || $type != 'register'){
?>
<h1>What your looking for can't be found!</h1>
<?php
}
if($type == 'login'){
?>
<h1>Login page:</h1>
<?php
}
if($type == 'register'){
?>
<h1>Register page:</h1>
<?php
}
?>
You have two problems in your code:
Your check for the error needs to use && and not ||. You want to see if login AND register are both not used.
You need to use if/else if statements to make sure only one condition is ever present.
Check this code out:
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
if($type != 'login' && $type != 'register'){
?>
<h1>What your looking for can't be found!</h1>
<?php
} else if($type == 'login'){
?>
<h1>Login page:</h1>
<?php
} else if($type == 'register'){
?>
<h1>Register page:</h1>
<?php
}
The problem is occurring because you are using a comparative operator in your check:
if($type != 'login' || $type != 'register')
Essentially, this is two separate conditions; the first will will evaluate to true when the page is not login, the second will evaluate to true when the page is not register. However, they're not checking for each other.
The first condition will consider register to be valid, as register is valid for if($type != 'login'). The second will consider login to be valid, as login is valid for if($type != 'register').
You need to ensure that neither of these pages are allowed, by using the AND comparator (&&): if($type != 'login' && $type != 'register')
However, for the matter, you don't need to worry about setting the $type variable at all. Simply check what _GET['type'] equates to at the same time that you're checking it's set:
<?php
if (isset($_GET['type']) && $_GET['type'] == 'login') {
?>
<h1>Login page:</h1>
<?php
}
else if (isset($_GET['type']) && $_GET['type'] == 'register') {
?>
<h1>Register page:</h1>
<?php
}
else {
header("location: ?type=login");
}
Also, please note that the mysql_ constructor is deprecated as of PHP 5.5, and is outright removed in PHP 7. Please consider switching to either MySQLi or PDO, ensuring that you also use parameterised queries to prevent SQL injection.
Hope this helps! :)
You just need to work out the logic a little more...if, else if, else. Case handling is the problem.
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else if(isset($_GET['type'] {
If ($_GET['type'] !== "") {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
}
......
?>
Sorry for the half baked answer, I'm on my phone and it's not very code-friendly

two conditions in if statement codeigniter

hi i am doing this login where i do if and else statement to check if the user can access that. i have this part where two or more types of user can access this part but not all users.. i have done
<?php foreach($_SESSION['datauser'] as $a){
if($a->USERTYPE == '1' || $a->USERTYPE == '2'){
?> do this
<?php }} ?>
but if it happens that for each of the condition that is true, it does the condition for example
<?php foreach($_SESSION['datauser'] as $a){
if($a->USERTYPE == '1' || $a->USERTYPE == '2'){
echo 'name';
}} ?>
if usertype == 1 and ==2 it outputs namename
i want is if usertype == 1 it will output name if usertype is == it will output also name, if usertype is both, it will also output name.
You can use break to end the loop once the condition is true once, e.g.
<?php foreach($_SESSION['datauser'] as $a){
if($a->USERTYPE == '1' || $a->USERTYPE == '2'){
echo 'name';
break;
}
}?>
try this
<?php
$a="";
foreach($_SESSION['datauser'] as $a){
if($a->USERTYPE == '1' || $a->USERTYPE == '2'){
$a='name';
}
}
echo $a;
?>
Since the since variable $_SESSION['datauser'] is having multiple value.
your foreach is looping multiple time hence answer is coming namename.
incase you want only one name to be print.
try the below code.
<?php $name="";
foreach($_SESSION['datauser'] as $a){
if($a->USERTYPE == '1' || $a->USERTYPE == '2'){
if($name==""){
$name="name";
}
}
}
$name
?>

PHP get parameter pulls in wrong values

I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}

$_SESSION value always empty

I conditionally embed one of two headers on a given page: either "Companies", or "Students and Professionals".
I store the user's type ('', 'student', or 'professional') in the $_SESSION array on login, then use a simple if statement to determine which header to embed.
The conditional (if) statement doesn't seem to work though; the first always evaluates as TRUE and company_home_header.php is always included, even when I've previously set $_SESSION['usertype'] to 'student' or 'professional'.
Why isn't the string stored in $_SESSION['usertype'] being evaluating correctly?
<?php
if ($_SESSION['usertype'] == "")
{
include('includes/company_home_header.php');
}
elseif ($_SESSION['usertype'] == "student" OR
$_SESSION['usertype'] == "professional")
{
include('includes/home_header.php');
}
?>
Please help.
Add session_start() on the top of your PHP code.
<?php
session_start();//<---Here
if($_SESSION['usertype'] == ""){
include('includes/company_home_header.php');
}
elseif($_SESSION['usertype'] == "student" || $_SESSION['usertype'] == "professional") {
include('includes/home_header.php');
}
?>
As stated, use session_start() and your expression is not correct:
elseif($_SESSION['usertype'] == "student" || $_SESSION['usertype'] == "professional") {
Try this:
<?php
session_start();
if($_SESSION['usertype'] == ""){
include('includes/company_home_header.php');
}
elseif($_SESSION['usertype'] == "student" or "professional") {
include('includes/home_header.php');
}
?>

Categories