This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am trying to show a different navigation bar depending on a users authority. Only problem is that when i log on to the system it shows the first else if, regardless of the authority of the user. To ensure that the problem is in the loop i have tried switching the else ifs and the same happened. the code is in an external php file and i call the function in the top of each page. any suggestions ?
function checkAuth() {
session_start();
if(!isset($_SESSION['role'])) {
require_once('menu.php');
} else if ($_SESSION['role'] = "registered") {
require_once('regnav.php');
} else if ($_SESSION['role'] = "admin") {
echo "FDGFGFD";
require_once('adminnav.php');
}
}
Your issue is with this part: $_SESSION['role'] = "registered". The single = means you are assigning the value "registered" to variable $_SESSION['role'].
If you are evaluating to check something, you need to use == i.e. $_SESSION['role'] == "registered"
You'll have the same issue with the second elseif
You need to use a double = sign for any condition check. For any condition check in if or else if, you have to use == in the middle of the variables.
If you use only = that means it assigning the value in the $_SESSION['role']. Also you can use === for checking the value as well as the type of the variable.
Valid function is:
function checkAuth()
{
session_start();
if(!isset($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
require_once('regnav.php');
}
else if ($_SESSION['role'] == "admin"){
echo "FDGFGFD";
require_once('adminnav.php');
}
}
?>
Related
I have this php code meant to compare two values,a variable $rate received from a form which can either have values 'applaud' or 'boo' so I want to check if the value is neither of it and kill the page with an error message.I've tried that but ...localhost can not handle this request
HERE IS MY CODE:
<?php
$rate=$_POST['rate'];
echo $rate;
?>
<?php
if($rate != 'applaud' OR $rate != 'boo')
{
die("Sorry there was a problem var rate was not well stated");
}
else
{
echo 'yay ,well stated!!!';
}
?>
I will apply some useful check to do so:-
<?php
if(!empty($_POST['rate'])){ //check data is coming or not actually
$rate= $_POST['rate'];
if($rate != 'applaud' && $rate != 'boo'){ // use && to check for neither of it
die("Sorry there was a problem var rate was not well stated");
}else{
echo 'yay ,well stated!!!';
}
}else{
die("POST data missing!");
}
?>
Replace OR with && and best way to compare two strings in php is strcmp()
if(strcmp($rate,"applaud")!=0 && strcmp($rate,"boo")!=0)
{
die("Sorry there was a problem var rate was not well stated");
}
If two strings are equal then it will return 0. If you want to learn more about strcmp() then you can visit here
I am working on vTiger 6.5 and I am trying to figure a way to see if a record exists in a custom module of mine. I want to check whether the 'policynumber' is new before saving, here is my code so far. For some reason it seems to act randomly depending on my module number chosen.
class isaHandler extends VTEventHandler {
function handleEvent($eventName, $entityData) {
global $adb;
$moduleName = $entityData->getModuleName();
if($moduleName=='isa'){
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$isNew = $entityData->isNew('policynumber');
if ($isNew == false) {
echo "Duplicate policy number";
exit;
}
}
if($eventName == 'vtiger.entity.beforesave') {}}
if($eventName == 'vtiger.entity.beforesave.final') {
$price = $entityData->get('currentamount');
if($price > 20000){
echo "Please go back and enter less than 20000";
exit;
}
if($eventName == 'vtiger.entity.aftersave') {}
}
}
At the moment I am currently using an echo just to see the result. But later on I will perform more than this.
isNew()
Returns true if new record is being created, false otherwise.
More info is here
you should write a custom query to check policynumber already exist or not in your function:
if($eventName == 'vtiger.entity.beforesave.modifiable') {
global $adb;
$result = $adb->pquery("SELECT your-field-name FROM table_name WHERE policynumber=?", array($policynumbervalue));
if($result && $adb->num_rows($result)) {
echo "This policy number exist";
die();
}else{
// write your overwrite code
}
} //end if($eventName == 'vtiger.entity.beforesave.modifiable')
Update:
I am assuming there is field i.e. policynumber in your form, you enter some value in this field and submit the form. so you will get entered policy number value from this:
$policynumbervalue = $entityData->get('policynumber'); //this is vtiger standard way
if this does not work, you can simply use php global variable $_REQUEST['policynumber'] but I is not a good practice.
Hope this will help.
This is the update to my answer, I simply done an if statement on the number of rows displayed.
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$policynumbervalue = $entityData->get('policynumber');
$sql = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?",array($policynumbervalue));
$nrows = $adb->num_rows($sql);
if($nrows > 0){
echo "<script type=\"text/javascript\">window.alert('ISA policy number already exists, you will be redirected to the updata module.');
window.location.href = '/vtigercrm/index.php?module=isa&view=List';</script>";
exit;
}
I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.
It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks
Are you looking for something like this. If so, it should work for your case:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers will apply only after it send to client. If you want immediately redirect, you can put exit(0) after header(...) in this case you are stop executing of the script and will send current headers to the browser which will redirect you.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
The problem is that you are comparing the "value" of isset (the result) with the value of your GET parameter, $id:
if(isset($_COOKIE['logged_in']) == $id)
What this says is "determine if $_COOKIE['logged_in'] is set and compare that determination to $id". PHP will evaluate isset, which returns true or false (as it says in the documentation), and compare that true or false to the other side of the expression (==), meaning $id, which will never match given your examples. If you query "random.com/test.php?id=true" (or false) that might do what you are looking for.
The line you have does not mean "determine if $_COOKIE['logged_in'] is set and compare the value of $_COOKIE['logged_in'] to the value of $id", which I believe is what you are looking for. In that case, what you want to do is first check that $_COOKIE['logged_in'] is set and then check that the value of $_COOKIE['logged_in'] matches $id, like so:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
If that doesn't make sense, here is a really explicit version that might be clearer as to what is actually going on:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
Hope that helps.
you should add another condition.
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
or use this script
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}
I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>
i'm trying to make an image appearance and disappearance based on 3 condition,
condition A = when user is logged in and it's username fits the displayname(by using the GET function) then it should echo "yes"
condition B = When user is logged in and it's username does not fits the displayname then it should echo "no"
condition C = when user is not logged in then it should echo "no" too
(i swapped the image with yes and no for easier referencing)
By logging in, the user has a cookie which is set like below
setcookie("user", $user, $expire);
setcookie("loggedin", 1, $expire);
First i get the cookie which i set when user logins.
$user1 = $_COOKIE["user"];
$loggedin = $_COOKIE['loggedin'];
$user = strtoupper($user1);
then i get my player's name
$playername = $_GET['player'];
Now i do the conditions
$uplayername = strtoupper($playername);
function showplusicon(){
global $uplayername;
if(($loggedin = "1") and ($user == $uplayername)){
echo "yes";
}
else if (($loggedin = "1") and ($user != $uplayername)){
echo "no";
}
else{
echo "no";
}
}
I don't see what's the problem but it keeps being registered as condition B.
Single equal signs assign, not compare.
if(($loggedin == "1") and ($user == $uplayername)){
...
And since you really only have two output states, you shouldn't need 3 conditions; remove condition B.
The variable $loggedin isn't known inside your function showplusicon(). You will need to add it as a global along with global $uplayername.
function showplusicon(){
global $loggedin, $uplayername;
// etc
}
Since this was accepted but not totally complete, I'll just add that as others indicated, the == equality operator needs to be used instead of the = assignment operator.
if(($loggedin == "1")
^^^^
$loggedin = "1"
Surely this should be:
$loggedin == "1"
Otherwise I would echo $user and $uplayername to see if these differ.
First thing's first:
$loggedin = "1" is a bad idea, as you're actually giving $loggedin the value "1" instead of comparing. Use == or even === if you're sure about the datatype.
Further on, the $loggedin isn't available in the scope of showplusicon(), as you haven't declared it as a global like you did with $uplayername.
Fix the listed issues above and it should be working a bit better.
If you've got problems to understand your own code's logic, a simple way is to assign the conditions to self speaking variables to get used to it:
$userIsLoggedIn = $loggedin == "1";
$userIsPlayer = $user == $uplayername;
The variables make it easy to debug your code at the very beginning
var_dump($userIsLoggedIn, $userIsPlayer);
so to locate the actual errors:
The variable $loggedin is undefined
The if clauses are setting a value (=), not comparing it (== or ===).
You can then use additionally a more readable code-flow to make your decision more visible:
if ($userIsLoggedIn)
{ // user is logged in
if ($userIsPlayer)
{ // user is player
...
}
else
{ // user is not player
...
}
}
else
{ // user is not logged in
...
}
Depending of what you want to output, this can be simplified even:
if ($userIsLoggedIn && $userIsPlayer)
{
echo 'yes';
} else
{
echo 'no';
}
Hope this is helpful for you.
Your main problem is todo with global scope of your variables:
<?php
//Get cookie info
$cookie['user'] = $_COOKIE["user"];
$cookie['loggedin'] = (isset($_COOKIE['loggedin'])&&$_COOKIE['loggedin']=='1')?TRUE:FALSE;
//Set user array
$user['user'] = strtoupper($cookie['user']);
$user['loggedin'] = $cookie['loggedin'];
$user['player'] = $_GET['player'];
$user['uplayername']=strtoupper($user['player']);
function showplusicon(){
//Made $user array available within function
global $user;
if($user['loggedin'] === TRUE && $user['user'] == $user['uplayername']){
echo "yes";
}else{
echo "no";
}
}
?>