Checking POST Value Codeigniter - php

This is what I am using to check if an post value is empty or not in codeigniter.
if($this->input->post('inputEmail')) { }
Just wanted to know what is the best method to check it, the above mentioned method or
$temp = $this->input->post('inputEmail');
if(!empty($temp)) { }

Check with isset in php to check whether variable is set or not.
Try this:
if(isset($tmp))
{
//it is set
}
else
{
//not set
}

Related

CodeIgniter session holds value 1 instead of true

In my codeigniter application I have specified a session as below
$this->session->isLoggedIn = true;
echo "Session";
echo $this->session->isLoggedIn;
even though I assign true the output shows as 1 instead. What have I done wrong here?
would really appreciate some help
EDIT :- The problem was actually in the following
When I try to acess this session from another method the session value is set to null. My other function is defined as
public function isLoggedIn()
{
var_dump($this->session->isLoggedIn);
if ((isset($this->session->isLoggedIn)=="true"))
{
return "true";
}
else
{
return "Sorry,false";
}
}
the vardump that I am printing in this isLoggedIn function is Null even though the session variable was assigned as displayed in the initial code above

PHP if SESSION exists but is null

How I can check in PHP if SESSION exists but is null.
I have really old self made platform, where visitor can log in as "guest", it sets $_SESSION "guest" but it is NULL.
Now I wan't to show some specific content to only registered. If I check
isset($_SESSION['guest']), is_null($_SESSION['guest']) or is_null($_SESSION['guest'] === FALSE they all return same.
How I can check if guest session exists but is null?
Try this:
if(isset($_SESSION['guest']) && is_null($_SESSION['guest']))
{
// your code
}
if($_SESSION['guest'] == "")
{
// code
}
You can try using empty instead, for example:
session_start();
if(isset($_SESSION['guest']) && !empty($_SESSION['guest'])) {
//Session is not null or empty
}
else
{
//Session is null or empty
}

Codeigniter form->run() equivalent to form_validation->run()?

I'm relatively new to CodeIgniter and this may be an obvious answer, but I am unable to find anything in the documentation.
I know how to use form_validation->run() to validate a form and run some code when it is submitted successfully. What if I wanted to do something with a form that did not require any sort of validation? Is there a form->run() equivalent that returns true when a user submits the form? Something like:
page.php
public function update($id)
{
$this->load->helper('form');
if($this->form->run())
{
// do some stuff after user has submitted form
}
}
I don't think there exists such method, but you can do it manually
For example:
if ( ! isset($_POST['something'])) // OR $this->input->post('something');
{
return false //maybe?
}
else
{
//$something = $_POST['something'];
return true //maybe?
}
A statement like this should work. You just need to check if any post data exists.
if ($_POST)
{
// do something
}

What is the proper way to test CodeIgniter session variable?

Take the following code snippet. What is the best way to test to make sure the session variable isn't empty?
<?php if ($this->session->userdata('userID')) {
$loggedIn = 1;
}
else {
$loggedIn = 0;
} ?>
If later in my script, I call the following, the first prints properly, but on the second I receive Message: Undefined variable: loggedIn
<?php echo $this->session->userdata('userID'));
echo $loggedIn; ?>
I've tried using !empty and isset, but both have been unsuccessful. I also tried doing the if/then statement backwards using if (!($this->session->userdata('userID')), but no dice. Any thoughts?
Try doing the following instead:
<?php
$loggedIn = 0;
if ($this->session->userdata('userID') !== FALSE) {
$loggedIn = 1;
}
?>
If the error continues, you'll need to post more code in case you're calling that variable in another scope.
If your aim is to see whether or not the session variable 'userID' is set, then the following should work:
$this->session->userdata('userID') !== false
Why don't you create a boolean field in your session called is_logged_in and then check like:
if(false !== $this->session->userdata('is_logged_in'))
if($this->session->userdata('is_logged_in')) {
//then condition
}
This is the proper way to test!

How to check whether your work on WP or WPMU?

I use global variable $table_prefix to differ whether I work on Word Press or WPMU. I need this global variable for my plugin. But is there any better way to check whether your pluggin is working on Word Press or WPMU?
In WPMU a global variable named wpmu_version should be set.
for my checks I use a function I found
// from http://frumph.net/wordpress/wordpress-plugin-theme-check-for-multisitewpmu/
// check for multisite. Returns boolean
function check_this_is_multsite() {
global $wpmu_version;
if (function_exists('is_multisite')){
if (is_multisite()) {
return true;
}
if (!empty($wpmu_version)){
return true;
}
}
return false;
}
use it like this
if(check_this_is_multsite()){
// is on wpmu
} else {
// is on single
}
You could define a constant:
e.g.
define('ENVIRONMENT', 'WP');
define('ENVIRONMENT', 'WPMU');

Categories