I'm trying to figure out how to add in a remember me functionality into my script with code igniter. Here's what I"m attempting to do with the checkbox. I'm getting a syntax error for the if statment. Am I not using it correctly?
<?php echo form_label(form_checkbox( 'remember', 1, if (isset($this->input->cookie('xtrcook'))) { TRUE } ) . ' Auto-login in future.', 'remember' ); ?>
You have a syntax error because you're trying to pass this as an argument:
if (isset($this->input->cookie('xtrcook'))) { TRUE }
That's not how expressions work in PHP. You want this to be TRUE is the xtrcook cookie is set, so just pass in this:
isset($this->input->cookie('xtrcook'))
Actually, $this->input->cookie() returns either the cookie value or FALSE, and both are always "set" so isset() is not appropriate here, you can use ! empty() or !== FALSE instead:
$this->input->cookie('xtrcook') !== FALSE // evals to TRUE if the cookie exists
The whole thing:
<?php echo form_label(form_checkbox('remember', 1, $this->input->cookie('xtrcook') !== FALSE).' Auto-login in future.', 'remember'); ?>
It is a bit complicated still, might be easier to pass on the form_label() function and just write this:
<label>
<?php echo form_checkbox('remember', 1, $this->input->cookie('xtrcook') !== FALSE); ?>
Auto-login in future
</label>
I don't think so, this looks like a view, and PHP is a server-side language, meaning that once it renders the page, it's done - it won't respond to any clicks on checkboxes... You could make an AJAX call to maintain your structure, but I suggest is to check for your remember variable on the page you submit the form to, and then set the cookie if needed.
Related
Was wondering if it is possible. My attempt is to have a dark mode checker within the PHP controller and send it back to the body as an if statement checking if the boolean is true or not.
Here's my current work:
Sender URL
Switch modes
Controller
public function switchModes()
{
//doesnt work since $isDark's value is not initialized
$isDark = !$isDark;
dd($isDark);
return $isDark;
}
Target result:
<body background="#if($isDark) #505050 #else #ffffff #endif">
The short answer is yes. But you should really try to make your question clearer.
If you want user to click a button or link to switch between light/dark modes, you should use Session.
In your current controller method, you commented that $isDark isn't initialized. You didn't provide a way to initialize that value anywhere, and your controller has no way to figure out what you are doing.
By using session variables, you can have values that persist between pages.
Learn more about sessions here: PHP Session Introduction
Session in Laravel works a little different, but the idea is the same. You want to fetch a session variable in your controller method, and check the value to determine if the user is under light or dark mode, then you would switch his mode by setting the session variable to the other value. In your view, you can check related session variable to determine the mode.
In your controller, you may want to do something like:
public function switchModes()
{
if (session()->has('isDark')) {
session()->put('isDark', !session('isDark'));
}
else {
//provide an initial value of isDark
session()->put('isDark', true);
}
return redirect()->route('your-route-name');
}
And in your view, you can check the session variable like this:
<body class="#if (session('isDark')) dark-mode #else light-mode">
I'm setting a cookie to stop certain things loading up on my page if I'm an admin user, and to do this I'm creating a cookie in php, reading it, and then echoing out the value via php on my page.
So my full code snippets are:
<?php
setcookie("preview", "true", time() - 3600); // Kills Existing
setcookie("preview", "true", time() + 3600); // Sets New
?>
-
<?php
if(isset($_COOKIE["preview"])){
$admin_preview = ($_COOKIE["preview"]);
}
else{
$admin_preview = "false";
}
?>
-
<?php
echo $admin_preview;
?>
So when I echo out $admin_preview, I'm expecting true to be the value, but instead I'm getting 1.
I'm using Firefox 62.0 so I can't view the actual cookie value, but I've obviously done something wrong. Any ideas where or how?
Do this instead :
$admin_preview = var_export($_COOKIE["preview"], true);
This will state that the value of the "preview" cookie is to be used as a string instead of a boolean.
In php boolean words (true/false) translate to 1 and 0 in that order, you have to explicitly state that the value is to be used as a string if that's what you wish for.
In PHP 1 means true, you can check it like if($admin_preview) //which will accept it as true,
also you can use filter var.
var_Dump(filter_var("TRUE", FILTER_VALIDATE_BOOLEAN));
//or
var_Dump(filter_var(1, FILTER_VALIDATE_BOOLEAN));
for more information about boolean in php you can check http://php.net/manual/tr/language.types.boolean.php
also you are not doing it wrong dude.
I was wondering if there's a better way than what I always do to check the value of a variable that might not be set? For example :
if(isset($_SESSION['super_user']) && $_SESSION['super_user'] == true) {
echo 'hi super user';
}
It would much simpler to just do this :
if($_SESSION['super_user']) {
echo 'hi super user';
}
But I would end up with a notice of undefined index. Is there another way of doing such kind of simple validation or I must stick with my actual way of doing it?
Thanks
if (!empty($_SESSION['super_user'])) {
// ...
}
You could initially set $_SESSION['super_user'] to false and only set it to true if the user is a super user. Then you can always use if ($_SESSION['super_user']) {}.
For the particular example your current code could be shortened to
if(!empty($_SESSION['super_user']))
however, the right way would be
if ($_SESSION['role'] == 'super_user')
I was wondering if there's a better way than what I always do to check the value of a variable that might not be set?
Yes. Always define all your variables. Like one I posted above. Always have a role element for all your users and there will be no need to check if it ever exists.
I’ve been trying to figure out how to use those flashdatas.
I remember having difficulties last time, and this time again, it seems that I forget something.
So basically, I’m trying to set up a flasherror somewhere :
if(!$this->paypal_pro->APICallSuccessful($PayPalResult['ACK']))
{
$this->session->set_flashdata('flashError',
array('Errors'=>$PayPalResult['ERRORS']));
redirect('main/form');
}
And in my main/form I got :
function Form()
{
// Process validation form
if ($this->form_validation->run() == FALSE)
{
//IF the validation process hasn't been run or there are validation errors
$this->parser->parse('template/template', $data);
}
And in that view, I’m trying to get that flashError :
<?php if($this->session->flashdata('flashError')):?>
<div class='flashError'>
<?php
$flashError=$this->session->flashdata('flashError');
foreach( $flashError['Errors'] as $Error) {
echo $Error['L_SHORTMESSAGE'].' ('.$Error['L_ERRORCODE'].'):';
echo '<br/>';
echo $Error['L_LONGMESSAGE'];
}
?>
</div>
<?php endif?>
I don’t have anything in that variable, and when I try to var_dump it, It returns me false.
Can someone explain me how to use it despite the official documentation saying “will only be available for the next server request, and are then automatically cleared”
From Codeigniters documentation:
If you find that you need to preserve a flashdata variable through an
additional request, you can do so using the keep_flashdata() function.
$this->session->keep_flashdata('item');
UPDATE:
Problem seems to be here:
$this->session->set_flashdata('flashError',
array('Errors'=>$PayPalResult['ERRORS']));
Try this one:
$this->session->set_flashdata(array('Errors'=>$PayPalResult['ERRORS']));
As you are doing
if($this->session->flashdata('flashError'))
You are actually removing the flashError item, as it has been read.
What you need to do, is as you have a little further down, assign it to a variable and then do your checks.
Although I visit php documentation , I didn't understand usage of "isset" function .
1 - for example in a php tutorial book author wrote a text with this context : when we create a form in a first.html and we want to use from form information in second.php , we must use these 2 pieces of code :
if(!isset($_GET['q']))
die("The Search key word is not set !!!");
$key = $_GET['q'];
if($key == "")
die("The Search key word must be entered !!!");
but I don't understand what is difference between these 2 codes ?
2 - for another example I saw this code for checking that a bottom is clicked or not :
if(isset($_POST['login_btn']))
{
....
}
but I don't understand why does it check that a bottom is clicked or not ?
$key="";
isset($key) //This evaluates to true.
The string is empty, but the variable is defined. The isset() function returns true, even for an empty string.
Perhaps you would like to use empty() instead, which has a broader range of conditions that evaluate to false.
isset checks to see if a given variable or index exists. If you assume that a variable exists and it doesn't, PHP generates a notice. All you're doing by using isset is suppressing possible notices -- which is good practice.
The difference is, in one instance the form was not submitted, in the second instance, the form was submitted, but with a blank value.
You wouldn't want to process a form on your page if the form has not been submitted yet.
There may also be multiple forms on one page, so you might check the button value to find out which form was submitted.
This function is quite simple
As on php.net:
Determine if a variable is set and is not NULL.
In your case, the isset function checks of your post variable is empty or not
$key == ""; // Is $key an empty string
isset($key); // Has the $key variable been defined and not null
And just for reference, here are a few others that may be useful
empty($key); // Is $key and empty value (0, false, null, "", array())
$key == false // Is $key a falsey value (0, false, null, "", "0", array())
$key === false // Is $key a false boolean (false)
I'm just going to dissect your code a little bit..
isset
if(!isset($_GET['q']))
die("The Search key word is not set !!!");
Would typically be used to see if a variable in a URL is set so something like this:
http://mysite.com/index.php?q=1
Would have $_GET['q'] set and isset($_GET['q']) would come back true
$key==""
$key = $_GET['q'];
if($key == "")
Would check to see if $key is empty. Using my previous example URL, $key would not be empty or blank, it would be 1.
Why check for a button press
The script that processes your form needs to know that it is being accessed after the form that way it does not error out. This is where you would want to make sure that the form submit button was pressed. As this is confusing, here is an example:
Say you want to insert a tag for your blogging system into a database you might have code that looks like this:
AddTag.php
<form name="addtag" method="process.php" action="post">
<input type="text" name="tagname" />
<input type="submit" name="submittag" />
</form>
process.php
<?php
if ($_POST['submittag']) {
//INSERT query
}
?>
As you can see, if process.php is accessed without the AddTag form being accessed first, the script would not try to insert your tag.
Everyone here has already explained to you what the isset() function checks for (that being if a variable is set or not), but I think what you're really asking about is how $_GET[] and $_POST[] work. You're going to need to look more at the form that feeds this code. Read about what $_GET[] and $_POST[] variables are and I think this code will make a lot more sense to you.
For instance, your second example checks to see if the value named login_btn was sent via post method. If it was, then it runs the ... code block.