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
?>
Related
So I have a button that I want to show and hide on certain parts.
So if $unlockcourse is false AND the $courseid is 1, I would like the button to be hidden, BUT I would like it to be shown at all other times no matter what the courseidis
$courseid = ($userinfo['course']) * 1;
$mycourse = ($userinfo['id_course']) * 1;
$unlockcourse = true;
if($haspaid == 1){
$unlockcourse = false;
} else if ($haspaid == 0) {
$unlockcourse = true;
}
<?php if ($unlockcourse == false && $mycourse >= 1) { ?>
Resume Course
<?php } ?>
I am surprised how did this not throw an error. The error lies here:
if ($unlockcourse == false) && if ($mycourse == 1) && {
It should be:
if ($unlockcourse == false && $mycourse == 1) {
So what you actually want to do is show it if unlockcourse is false, and mycourse is 1. This means that to find out when to show it, you need to negate both conditions like this:
<?php if (!($unlockcourse == false && $mycourse == 1)) { ?>
Resume Course
<?php } ?>
Try Editing the following code
<?php if ($unlockcourse == false && $mycourse >= 1) { ?>
Resume Course
<?php } ?>
into
<?php if !($unlockcourse == false && $mycourse >= 1){
echo "<a href='course-work-proc.php' class='btn btn-primary'>Resume Course</a>";
}?>
This will make the button appear when your statements are true, and nothing will appear when the statement is false
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
}
}
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') {
//...
}
if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
echo "YES";
} else {
echo "NO";
}
}
in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?
This is likely what you are looking for
UPDATE - I just changed || to && for the last condition in case you were quick to try it out.
if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
echo "YES";
} else {
echo "NO";
}
If you have to write a lot of conditionals you could use one of the following:
Using a foreach and a conditional:
$either_abc_is_x = function() {
$keys = ['a','b','c'];
foreach($keys as $key)
if(isset($_GET[$key]) && $_GET[$key] == "x")
return true;
return false;
};
echo $either_abc_is_x() ? 'YES' : 'NO';
Using a an array filter with a conditional:
$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
return isset($_GET[$v]) && $_GET[$v] == 'x';
});
echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';
Array gymnastics:
$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));
echo $either_abc_is_x ? 'YES' : 'NO';
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');
}
?>