Php function include - php

So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!

You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }

you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.

You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}

Related

I am trying to use if within another if statement working but i am getting some error

I want try if within if loop use but getting some error black page no error show please check its and help me
public function login2($otp) {
$this->db->where('otp', $otp);
$query = $this->db->get('login');
if ($query->num_rows() == 1)
{
//$this->load->view('index');
if ($role=='1' && $type=='1')
{
$this->load->view('index');
}
else if ($role=='2' && $type=='1')
{
$this->load->view('admin/index');
}
}
else
{
$_SESSION['error']='Wrong OTP Plese Enter correct OTP';
$this->load->view('otp');
}
}
Can you echo the contents of the variables used in the if conditions? Check they match your statements. Unless they're global, I can't see where they're populated.

Getting the url where the request is redirected from in yii2?

Sorry for my English, but what I'm trying to say is explained below.
I have a controller say ControllerCard which has an action like this.
function actionScanCard()
{
...
$this->redirect('/transaction/redeem');
...
}
In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card
function actionRedeem()
{
$redirectFrom = ????;
if ($redirectFrom === '/card/scan-card')
{
// some actions
}
else
throw new ForbiddenHttpException('Must scan card!');
}
How do I get this $redirectFrom value with Yii2?
You could use the remember() & previous() methods in yii\helpers\BaseUrl.
function actionScanCard()
{
...
\yii\helpers\Url::remember();
$this->redirect('/transaction/redeem');
...
}
in TransactionController (or other)
function actionRedeem()
{
$url = \yii\helpers\Url::previous();
if($url === Url::to('card/scan-card')) {
// some actions
} else{}
}

Integer is returned instead of boolean in php

I hope you are doing great. I have a class in my project with various methods. The thing that happens is, one of the methods is supposed to return a Boolean variable. Instead. It returned the number "1" instead of false. If you could tell me where is the problem.
Thanks in advance, Cheers.
Useful pieces of my code:
Class method:
public function validatePwd($pwd1, $pwd2) {
if (strcasecmp($pwd1, $pwd2) == 0)
{
return true;
}
else
{
return false;
}
}
The script that executes it:
$check1 = $user->validatePwd($Password, $Password1);
echo $check1;
if ($user->validatePwd($Password, $Password1))
{
}
else
{
$errors[] = 'Error!, passwords entered are not compatible, Please'
. ' enter passwords that match each other';
}
If you do var_dump($check1) instead of echo $check1; it should show as boolean.

Calling function in another function

I have a function printContent() which prints the arguments and logged() which checks if the user is logged in.
My point is to do something like this:
logged("printContent('TITLE', 'CONTENT', 1)", 1);
It doesn't work. It should printContent() if user is not logged in, but nothing is happening. If I'll try changing return() into print() it prints the text "printContent(text...)".
Here are these two functions:
function logged($echo = 0, $logout = 0)
{
if($_SESSION['user'])
{
if($echo)
{
if(!($logout)) return $echo;
else return false;
}
else return true;
}
else
{
if($logout == 1) return $echo;
else return false;
}
}
function printContent($title, $content, $type = 0){
if($type == 1){
echo '<div class="right-box">';
if($title) echo '<h3>'.$title.'</h3>';
echo $content.'</div>';
}
else {
echo '<div class="left-box">';
if($title) echo '<h2>'.$title.'</h2>';
echo '<div class="left-box-content">'.$content.'</div></div>';
}
}
It should printContent() if user is not logged in
You can try
if(!logged())
{
printContent('TITLE', 'CONTENT', 1);
}
Your logged() function is too complicated; the $echo and $logout parameters make the logic extremely hard to follow. You should simplify it to just this, doing one thing very well:
function isLoggedIn()
{
return !empty($_SESSION['user']);
}
Then, the logic becomes quite simple afterwards:
if (!isLoggedIn()) {
printContent('title', 'content', 1);
}
Play time
Being fancy, you could do this since 5.3, though it's a very contrived example of what you could accomplish with anonymous functions:
function ifLoggedIn($loggedIn, $loggedOut)
{
return empty($_SESSION['user']) ? $loggedIn() : $loggedOut();
}
The $loggedIn and $loggedOut parameters are the callback parameters and get executed from inside the function, based on whether the user is logged in or not. To use it:
ifLoggedIn(function() {
}, function() {
printContent('TITLE', 'CONTENT', 1);
});
What you seem to be looking for is a callback.
If I understand you correctly, you wish printContent only to execute, if the user is logged in, correct?
You may want to check this question for further info: How do I implement a callback in PHP?
you are trying to execute a plain string. Use
function1( function2() );
that is the same as
$tmp = function2();
function1($tmp);

$_SESSION never getting set

I'm working on an assignment on a PHP course, and I'm stucked at the last part of it.
The assignment is to create a simple login form and use a session as well as hardcoded usernames and passwords (i.e. no db).
What I have problems with is the class that handles the login, and sessions especially. There's a lot of code and I didn't know what I could remove and therefore I've put it on Pastebin instead, hope that's alright.
Thing is that the unit tests that's built into the class passes except for nr. 4, the one that's checking that the user is logged in. The problem seems to be that $_SESSION[$this->loginSession] doesn't get set, and this is what I need help with.
The variable $loginSession is declared in the beginning of the class, and should be set to "isLoggedIn" when a user types a correct username and password, but that doesn't happen (no error message).
My class is:
<?php
class LoginHandler {
private $loginSession;
public function IsLoggedIn() {
if($_SESSION[$this->loginSession] == "isLoggedIn") {
return true;
}
else {
return false;
}
}
public function DoLogin($username, $password){
if ($username != null && $password != null){
switch ($username){
case "hello";
if ($password == "1234"){
$_SESSION[$this->loginSession] == "isLoggedIn";
return true;
}
else return false;
case "hello2";
if ($password == "12345"){
$_SESSION[$this->loginSession] == "isLoggedIn";
return true;
}
else return false;
}
}
else {
return false;
}
}
public function DoLogout(){
unset($_SESSION[$this->loginSession]);
}
public function Test() {
$this->DoLogout();
// Test 1: Check so you're not logged in.
if($this->IsLoggedIn() == true){
echo "Test 1 failed";
return false;
}
// Test 2: Check so that it's not possible to login with wrong password.
if ($this->DoLogin("hello", "4321") == true){
echo "Test 2 failed";
return false;
}
// Test 3: Check that it's possible to log in.
if ($this->DoLogin("hello", "1234") == false){
echo "Test 3 failed";
return false;
}
// Test 4: Check that you're logged in
if ($this->IsLoggedIn() == false){
echo "Test 4 failed";
return false;
}
return true;
}
}
?>
I hope it's enough to include the class and not all the other files, otherwise I'll put them up.
Now I see it :-)
$_SESSION[$this->loginSession] == "isLoggedIn";
== should be =
== compares while = sets
You need to start the session. session_start(); Place it at the very top of the documents (only one time on a page load) you are using.
$this->loginSession is never set so it's NULL
$_SESSION[null] is not possible as far as i know
change your code to
private $loginSession = 'testing';
and it should work
Why do you put semicolon in your case instruction case "hello";There should be a colon. case "hello": { ...instructions}

Categories