Check session is set, codeigniter - php

I have a upload script which post upload lets the user edit the content title for a period of time. It sets the following in the ci_sessions user_data column in the db:
array (
'user_data' => '',
'edit' =>
array (
'image_id' => 'HF',
'session_id' => '783c15b057bcd9c19d3fd82f367ee55d',
),
)
The problem is my session CHECK code can't find the session:
<?php if ($this->session->userdata('edit') !== FALSE) : ?>
<?php echo '<!-- session found -->'; ?>
<?php $session_info = $this->session->userdata('edit'); ?>
<?php $ids_array = explode(",", $session_info['image_id']); ?>
<?php foreach ($ids_array as $id): ?>
<?php
if ($id == $alpha_id
&&
$session_info['session_id'] == $this->session->userdata('session_id')) :
?>
The echo on line 2 of that block never gets outputted.
Can anyone see what I'm doing wrong? thanks
heres my controller http://pastebin.com/aXeRn1VN

Try this
<?php if( $this->session->userdata('edit') ) : ?>
instead of
<?php if ($this->session->userdata('edit') !== FALSE) : ?>

Related

Add more data into cookie in PHP

I have a cookie and I'm storing multiple checked checkbox values of page1 in this. Now I want to add more checked checkbox values into the cookie of page2 and then page3 and so on (I'm using pagination in my code). How can I do that in PHP?
<?php
$cookiename = "emp";
if (isset($_GET['check_list']) ) {
setcookie($cookiename , implode(',' , $_GET['check_list']) ,
time()+(86400/86400*30) );
}
?>
//to print the values
<?php
if (isset($_COOKIE[$cookiename]) ) {
print_r($_COOKIE);
}
?>
Maybe in this way:
<?php
$page = $_GET['page'];
$cookiename = "emp";
// GET COOKIE TO NOT OVERWRITE.
$cookie = isset($_COOKIE[$cookiename]) ? unserialize($_COOKIE[$cookiename]) : [];
if (isset($_GET['check_list']) ) {
$cookie[$page] = $_GET['check_list'];
setcookie($cookiename , serialize($cookie), time()+(86400/86400*30) );
}
?>
//to print the values
<?php
if (isset($_COOKIE[$cookiename]) ) {
print_r(unserialize($_COOKIE[$cookiename]));
}
?>
With unserialize you can store a multiple dimension array into a cookie with no problem.

PHP - If an array contains part of a string

I have the following PHP code:-
<?php
if( have_rows('postcode_checker', 'option') ):
while ( have_rows('postcode_checker', 'option') ) : the_row(); ?>
<?php
$postcodes .= get_sub_field('postcodes');
$postcode_url = get_sub_field('page');
?>
<?php endwhile;
else : endif;
$postcode_array = $postcodes; // This collects postcodes, i.e. LE67, LE5 etc...
$postcode_array = explode(',', $postcode_array);
$postcode_array = str_replace(' ', '', $postcode_array);
$postcode_search = $_POST['postcode']; // This will be a single postcode i.e. LE67
if (in_array($postcode_search, $postcode_array)) {
echo 'yes';
} else {
echo 'no';
}
?>
So the code above is working fine if I want to look up say LE67 and it finds LE67 in the array and returns 'yes'. Now if I search LE675AN for example it will return no even though it needs to be returning yes as it is within the postcode area.
Any idea's on how I can achieve this?

Very simple geo targeting for WordPress

I need to do some very basic geo-targeting for a WordPress site.
Something like:
<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>
Till now I used the GEO service provided by ip-api.com.
My code looks like this:
<?php $ip = $_SERVER['REMOTE_ADDR'];
$query = #unserialize(file_get_contents('http://ip-api.com/php/' . $ip));
$geo = $query['countryCode'];
if( $query['countryCode'] == 'US') : ?>
DO THIS
<?php else: ?>
DO THAT
<?php endif ?>
The problem is that the php unserialize is now deprecated and extremely slow. Ip-api suggest to use JSON instead. But I am a total newbie and I don't know how to achieve the same results with JSON. Can someone please help me?
I know there are various plug-ins out there, but I think they are overkill for the very simple geo targeting I need.
PS: I just learn that I should use the code
$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});
But still I need help to put together the final complete code.
Try this:
<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
$_SERVER['REMOTE_ADDR'] : '';
$country_code = '';
if ( $ip ) {
// 'fields' are comma-separated list of fields to include in the response.
// No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
$url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';
$res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
$geo = json_decode( $res_body );
$country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}
if ( 'US' === $country_code ) : ?>
DO THE THIS
<?php else : ?>
DO THE THAT
<?php endif; ?>
PS: I just learn that I should use the code
Yes, that's an example of doing it with jQuery.

JSON from PHP file result was code, do not text

I have code PHP in file update_customer.php to save information customer
<?php
date_default_timezone_set('Asia/Ho_Chi_Minh');
$response = array( 'status' => 0, 'message' => '', 'typeinsurance' => '', 'thoigianmua' => 0 );
if (empty($_GET) && !empty($_POST) && count($_POST) == 7 && isset($_POST['fullname']) && isset($_POST['email']) &&
isset($_POST['address']) && isset($_POST['phone']) && isset($_POST['cost']) && isset($_POST['typeinsurance']) &&
isset($_POST['typepay']))
{
require_once ('./../include/database.php');
$database = new Database();
$time = time();
$_POST['thoigianmua'] = $time;
if ($database->insert('customer', $_POST)){
$response['status'] = 1;
$response['typeinsurance'] = $_POST['typeinsurance'];
$response['thoigianmua'] = $time;
}
}
echo json_encode($response);
and code JQUERY to sent data, but My JSON get
Object {status: 1, message: "", typeinsurance: "<?php echo $_POST['typeinsurance']; ?>", thoigianmua: 1440642246}
Please help me, thank in advance
You php code has error for sure that's why its returning the data in a way that you not expected.
The wrong part is <?php echo $_POST['typeinsurance']; ?> you have given the php code as string.
you have to check your php file

Multiple IF php with 2 else statements

I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here's the code I have now -
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
} else {
echo '<li>Register your Site</li>';
}
}
}
}
} else {
echo '<li>UPGRADE</li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
Free user sees a UPGRADE link
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it's suppose to echo the register link -- Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the Wordpress MU documentation, I would guess that the get_blogs_of_user always returns an array, so checking on the value of $blogs exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:
<?php
if (pmpro_hasMembershipLevel(array(2,4))) {
if (current_user_can( 'edit_posts' )) :
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
/*Check if we got an array back and, if so,
check if it has elements*/
if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
foreach ( $blogs as $blog ) :
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain
. $blog->path
.'wp-admin/">My Site</a></li>';
}
endforeach; // end foreach loop
} else {
echo 'Register your Site';
} // end if $blogs
endif; // endif current_user_can
} else {
?>
<div>UPGRADE</div>
<?php
}
?>
Try this :
<?php if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
} else {
echo 'Register your Site';
}
}
} else { ?>
<div>UPGRADE</div>
<?php } ?>
Give this one a shot. Even if it doesn't work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole #JustinPearce's method of checking if the user has a blog from his answer
<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );
$registerLink = 'Register your Site';
$upgradeLink = '<div>UPGRADE</div>';
function echoBlogLinks($blogs) {
echo '<ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
echo '</ul>';
}
if ($has_membership_level) {
if ($has_blog) {
echoBlogLinks($blogs);
} else {
echo $registerLink;
}
} else {
echo $upgradeLink;
}

Categories