Retrieve Cookie Data in CodeIgniter - php

I have the following function :
public function check_daily_data() {
$month = $this->input->post('month');
echo 'Month :' . $month . '</br>';
$year = $this->input->post('year');
echo 'Year :' . $year . '</br>';
$user_id = $this->session->userdata('employee_id');
echo 'User Id : ' . $user_id . '</br>';
//load the cookie helper
$this->load->helper('cookie');
setcookie("month", "", time()-3600);
setcookie("year", "", time()-3600);
$time = time()+3600;
$cookie_month = array(
'name' => 'cookie_month',
'value' => $month,
'expire' => '3600',
'secure' => TRUE
);
$this->input->set_cookie($cookie_month);
$cookie_year = array(
'name' => 'cookie_year',
'value' => $year,
'expire' => '3600',
'secure' => TRUE
);
$this->input->set_cookie($cookie_year);
var_dump($this->input->cookie('cookie_month'));
$cookie_months = $this->input->cookie('cookie_month',TRUE);
echo 'Cookie Month : ' . $cookie_months . '</br>';
$cookie_years = $this->input->cookie('cookie_year',TRUE);
echo 'Cookie Year : ' . $cookie_years . '</br>';
}
When I run the script and check the cookie variable using FireBug, I can be able to view the cookie data but when I try to echo it, I get a blank value.
The var_dump returns a false, please can you advise what am I doing wrong?

well, why you are not getting value is because, you have set 'secure' => TRUE. you need to set it up to false. the true setting will work only with HTTPS
$cookie_month = array(
'name' => 'cookie_month',
'value' => $month,
'expire' => '3600',
'secure' => FALSE
);
$cookie_year = array(
'name' => 'cookie_year',
'value' => $year,
'expire' => '3600',
'secure' => FALSE
);
then you will get your value when you call
$cookie_months = $this->input->cookie('cookie_month',TRUE);

Related

How to export 20 million records in pdf?

I need to export 20 million data in a pdf file. I have used the "WkHtmlToPdf" package
(https://github.com/mikehaertl/phpwkhtmltopdf)
in my yii2 application. I have used Mongo database, I have created a report module in which I need to export the pdf with large data.
Code is working fine but it is taking too much time to write 1,00,000 it takes approx 10 mins so for 20 million approx it takes 20 hours so we are looking for a solution to write 20 million data.
Below is my query to fetch data from mongo:
$query = CannedReport::find()->where(['isParent'=>1]);
$this->load($params);
if (in_array(Yii::$app->user->identity->user_type, ['tenant', 'subtenant'])) {
$query->andWhere(['tm_id' => (string)Yii::$app->user->identity->tm_id]);
// $match['tm_id'] =(string)Yii::$app->user->identity->tm_id;
}
$query->andWhere(['!=', 'callstatus', 'completed']);
$query->andWhere(['hangup' => 'ORIGINATOR_CANCEL']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
'sort' => [
'defaultOrder' => ['start_epoch' => SORT_DESC],
],
]);
if (isset($this->em_id) && !empty($this->em_id)) {
$query->andFilterWhere(['in', 'em_id', $this->em_id]);
}
foreach (['start_epoch', 'end_epoch'] as $epoch) {
if ($epoch == 'start_epoch') {
if (isset($this->start_epoch) && !empty($this->start_epoch)) {
$date = explode(' - ', $this->start_epoch);
} else {
$date[0] = date('Y-m-d H:i:s', strtotime('today - 31 days'));
$date[1] = date('Y-m-d') . " 23:59:59";
$this->start_epoch = implode(' - ', $date);
}
} else {
$date = explode(' - ', $this->{$epoch});
}
if (isset($date[0]) && isset($date[1]) &&
Yii::$app->helper->validateDate($date[0], 'Y-m-d H:i:s') && Yii::$app->helper->validateDate($date[1],
'Y-m-d H:i:s')
) {
$query->andFilterWhere([
'>=',
$epoch,
(string)Yii::$app->helper->currentTimezone($date[0], 'Y-m-d H:i:s')
]);
$query->andFilterWhere([
'<=',
$epoch,
(string)Yii::$app->helper->currentTimezone($date[1], 'Y-m-d H:i:s')
]);
}
unset($date);
}
return $dataProvider;
Pdf generation code
$pdf = new Pdf(array(
'binary' => '/usr/local/bin/wkhtmltopdf',
'ignoreWarnings' => true,
'commandOptions' => array(
'useExec' => true, // Can help on Windows systems
'procEnv' => array(
'LANG' => 'en_US.utf-8',
),
),
));
$options = array(
'dpi' => 96,
'image-quality' => 100,
'disable-smart-shrinking',
'no-outline',
);
$pdf->setOptions($options);
$pdf->addPage($tableHtml);
$detailHeadingHtml = '<div class="row" style="padding-top: 5px;"><h3 style="text-align: center; padding: 5px;">Detailed Records-Call Direction Report</h3><div>';
$pdf->addPage($detailHeadingHtml . '' . $abandonedCallsHtml . '' . $completedCallsHtml . '' . $failedCallsHtml . '' . $transferredCallsHtml . '<table><tr><td align="center"><b>Total Count:</b></td><td align="center"><b>' . $dataProvider->query->count() . '</b></td></tr></table>');
if (!$pdf->send($fileName)) {
$error = $pdf->getError();
echo $error;
}
exit();
Is there any optimized way to create a pdf with such large data using yii2 and mongo?

yii2 doesnot store Cookie in localhost or server

I write this below code in a different controller to store cookies
$cookies = Yii::$app->response->cookies;
$number=0;
if($cookies->has('registration_id')){
if($cookies->has('registration_attempt')){
$registration_att = $cookies->getValue('registration_attempt');
$number = $registration_att+1;
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number,
'expire' => time() + 86400*2,
]));
}
else{
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number,
'expire' => time() + 86400*2,
]));
}
} else{
$cookies->add(new Cookie([
'name' => 'registration_id',
'value' => 'Generate Id',
'expire' => time() + 86400*2,
]));
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number+1,
'expire' => time() + 86400*2,
]));
//**show here**
}
I set secure true but it does not show anything in different controller . When i am trying to access cookies value it shows null. Cookie value set but it doesn't set globally . Instant show cookie value where I commented show here. I am checking here if cookie set then if either else and always hit that. And if echo in that place it show data .

php array page system, not working

So I'm working on setting up my first "fancy" page system, and I have run into a problem. I use the code seen below, and it loads the "profile" page without any problems, and the default page works fine too. However the other two pages does not show at all, and I can't seem to request them in the URL eighter. All the files are there tho. Any help here will be much appreciated! :)
if(isset($_SESSION['user_id'])){
require('user.php');
$player = new user($_SESSION['user_id'], $database);
$default_page = 'profile';
$pages = array(
'profile' => array(
'name' => 'Profile',
'file' => 'profile.php',
'function' => 'profile',
),
'create_monster' => array(
'name' => 'Create Monster',
'file' => 'monsterPages.php',
'function' => 'createMonster',
),
'create_attack' => array(
'name' => 'Create Attack',
'file' => 'attackPages.php',
'function' => 'createAttack',
),
);
if(!empty($_GET['page'])){
$page = strtolower(trim($_GET['page']));
if(isset($pages[$page])){
require($pages[$page]['file']);
echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
$pages[$page]['function']();
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
You've got some redundancy in your code. You could abbreviate it.
$page = isset($_GET['page']) ? $_GET['page'] : $default_page;
// You should check that the page exists here.
require($pages[$page]['file']);
echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
$pages[$page]['function']();
I suggest you place the page loading code in a function. I've created a simple class here:
<?php
$pages = array(
'profile' => array(
'name' => 'Profile',
'file' => 'profile.php',
'function' => 'profile',
),
'create_monster' => array(
'name' => 'Create Monster',
'file' => 'monsterPages.php',
'function' => 'createMonster',
),
'create_attack' => array(
'name' => 'Create Attack',
'file' => 'attackPages.php',
'function' => 'createAttack',
),
);
class PageLoader
{
public $pages;
public $base_path;
public function __construct($pages, $base_path)
{
$this->pages = $pages;
$this->base_path = $base_path;
}
public function run($name) {
if(! isset($this->pages[$name])) {
throw new Exception('Page not found');
}
$path = $this->base_path . DIRECTORY_SEPARATOR . $this->pages[$name]['file'];
$func = $this->pages[$name]['function'];
require_once $path;
call_user_func($func);
}
}
$page = isset($_GET['page']) ? $_GET['page'] : 'profile'; // If no page given default to profile.
$loader = new PageLoader($pages, '/tmp');
$loader->run($page);
Before you can use $_SESSION, you need to start the session:
session_start(); # this should be the very 1st line in your php
if(isset($_SESSION['user_id'])){
// ...
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
and make sure that $pages[$default_page]['function'](); sets $_SESSION['user_id']

Gravity Forms - gform_after_submission not working

gform_after_submission is getting called in my page, but the $entry and $form objects are null. Is there any reason why that would be happening? Based on the output of the logs, I know the code runs, but can't figure out why the $entry argument is null.
add_action('gform_after_submission_2', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
error_log("Posting comments form");
$post_url = 'https://api.club-os.com/prospects?clubLocationId=686';
$body = array(
'first_name' => $entry['7.3'],
'last_name' => $entry['7.6'],
'email' => $entry['6'],
'mobilePhone' => $entry['8']
);
error_log('Before Post to' . $post_url);
$args = array(
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' )),
'body' => $body,
'sslverify' => false
);
foreach ($body as $key => $value) {
error_log($value . " in " . $key . ", ");
}
$request = new WP_Http();
$response = $request->post($post_url, $args);
}
Figured it out.
'first_name' => $entry['7.3'],
'last_name' => $entry['7.6'],
The 7.3 and 7.6 were incorrect indexes to the $entry object. I just used '7' and the split function to get it to work correctly.

Can this PHP function be improved?

Below is some code I am working on for a navigation menu, if you are on a certain page, it will add a "current" css class to the proper tab.
I am curious if there is a better way to do this in PHP because it really seems like a lot of code to do such a simple task? My pages will also have a jquery library already loaded, would it be better to set the tab with jquery instead of PHP? Any tips appreciated
<?PHP
active_header('page identifier goes here'); //ie; 'home' or 'users.online'
function active_header($page_name)
{
// arrays for header menu selector
$header_home = array('home' => true);
$header_users = array(
'users.online' => true,
'users.online.male' => true,
'users.online.female' => true,
'users.online.friends' => true,
'users.location' => true,
'users.featured' => true,
'users.new' => true,
'users.browse' => true,
'users.search' => true,
'users.staff' => true
);
$header_forum = array('forum' => true);
$header_more = array(
'widgets' => true,
'news' => true,
'promote' => true,
'development' => true,
'bookmarks' => true,
'about' => true
);
$header_money = array(
'account.money' => true,
'account.store' => true,
'account.lottery' => true,
'users.top.money' => true
);
$header_account = array('account' => true);
$header_mail = array(
'mail.inbox' => true,
'mail.sentbox' => true,
'mail.trash' => true,
'bulletins.post' => true,
'bulletins.my' => true,
'bulletins' => true
);
// set variables if there array value exist
if (isset($header_home[$page_name])){
$current_home = 'current';
}else if (isset($header_users[$page_name])){
$current_users = 'current';
}else if (isset($header_forum[$page_name])){
$current_forum = 'current';
}else if (isset($header_more[$page_name])){
$current_more = 'current';
}else if (isset($header_money[$page_name])){
$current_money = 'current';
}else if (isset($header_account[$page_name])){
$current_account = 'current';
}else if (isset($header_mail[$page_name])){
$current_mail = 'current';
}
// show the links
echo '<li class="' . (isset($current_home) ? $current_home : '') . '"><em>Home</em></li>';
echo '<li class="' . (isset($current_users) ? $current_users : '') . '"><em>Users</em></li>';
echo '<li class="' . (isset($current_forum) ? $current_forum : '') . '"><em>Forum</em></li>';
echo '<li class="' . (isset($current_more) ? $current_more : '') . '"><em>More</em></li>';
echo '<li class="' . (isset($current_money) ? $current_money : '') . '"><em>Money</em></li>';
echo '<li class="' . (isset($current_account) ? $current_account : '') . '"><em>Account</em></li>';
echo '<li class="' . (isset($current_mail) ? $current_mail : '') . '"><em>Mail</em></li>';
}
?>
The two very large blocks of code at the bottom could be reduced drastically to a simple loop:
<?php
foreach (array('home', 'users', 'forum' /* ... */ ) as $item) {
$ar = "header_$item";
echo '<li class="', (isset($$ar[$page_name]) ? 'current' : '')
, '"><em>', ucword($item), '</em></li>';
}
?>
You should try not to print <li class=""> or something like that; it looks messy. I've moved the checking of whether this page or not is the one to highlight to a seperate function in case you end up changing the layout of $applicable_list.
<?php
function active_header($page) {
$applicable_list = array(
"home" => array("home"),
"users" => array(
"users.online", "users.online.male", "users.online.female", "users.online.friends",
"users.location", "users.featured", "users.new", "users.browse", "users.search", "users.staff"
),
"forum" => array("forum"),
"more" => array("widgets", "news", "promote", "development", "bookmarks", "about"),
"money" => array("account.money", "account.store", "account.lottery", "users.top.money"),
"account" => array("account"),
"mail" => array("mail.inbox", "mail.sentbox", "mail.trash", "bulletins.post", "bulletins.my", "bulletins")
);
$pages = array_keys($applicable_list);
function is_active_page($page, $category, $category_pages_list) {
return array_key_exists($category, $category_pages_list) && in_array($page, $category_pages_list[$category]);
}
foreach($pages as $key => $category) {
printf('<li%s><em>%s</em></li>' . "\n",
(is_active_page($page, $category, $applicable_list) ? ' class="current"' : ''),
ucwords($category)
);
}
}
?>
May as well throw in my lot. Output is restricted to match that given in the original question.
<?PHP
active_header('page identifier goes here'); //ie; 'home' or 'users.online'
function active_header($page_name)
{
// Unified array
$headers = array(
'Home' => array('home' => true),
'Users' => array(
'users.online' => true,
'users.online.male' => true,
'users.online.female' => true,
'users.online.friends' => true,
'users.location' => true,
'users.featured' => true,
'users.new' => true,
'users.browse' => true,
'users.search' => true,
'users.staff' => true
),
'Forum' => array('forum' => true),
'More' => array(
'widgets' => true,
'news' => true,
'promote' => true,
'development' => true,
'bookmarks' => true,
'about' => true
),
'Money' => array(
'account.money' => true,
'account.store' => true,
'account.lottery' => true,
'users.top.money' => true
),
'Account' => array('account' => true),
'Mail' => array(
'mail.inbox' => true,
'mail.sentbox' => true,
'mail.trash' => true,
'bulletins.post' => true,
'bulletins.my' => true,
'bulletins' => true
)
);
foreach($headers as $header => &$pages) {
echo '<li class="';
if(isset($pages[$page_name])) echo 'content';
echo '"><em>', $header, '</em></li>';
}
}
?>
I'm not a fan of mixing up the code with the output, but it'll do for example.
PHP hint of the day: Don't use string concatenation if you're just echoing a string
Rather than using the page names as array keys you could simply have arrays of the page names, and then compare using in_array($page_name, $array), rather than isset($array[$page_name]).
This should happily work alongside the alterations from #meager, and would allow the static bits of code at the top to shrink a little.
Consolidate your arrays, or put all that logic in another well-named function. My example consolidates the arrays.
// it's ugly, but at least the ugliness
// is confined to only _one_ array ;)
$map_pages_to_navitem = array(
'home' => 'home',
'users.online' => 'users',
'users.online.male' => 'users',
'users.online.female' => 'users',
'users.online.friends' => 'users',
'users.location' => 'users',
'users.featured' => 'users',
'users.new' => 'users',
'users.browse' => 'users',
'users.search' => 'users',
'users.staff' => 'users',
'forum' => 'forum',
'widgets' => 'more',
'news' => 'more',
'promote' => 'more',
'development' => 'more',
'bookmarks' => 'more',
'about' => 'more',
'account.money' => 'money',
'account.store' => 'money',
'account.lottery' => 'money',
'users.top.money' => 'money',
'account' => 'account'),
'mail.inbox' => 'mail',
'mail.sentbox' => 'mail',
'mail.trash' => 'mail',
'bulletins.post' => 'mail',
'bulletins.my' => 'mail',
'bulletins' => 'mail',
);
$current = $map_pages_to_navitem[$page_name];
echo '<li class="'.($current=='home')?'current':''.'"><em>Home</em></li>';
echo '<li class="'.($current=='users')?'current':''.'"><em>Users</em></li>';
echo '<li class="'.($current=='forum')?'current':''.'"><em>Forum</em></li>';
echo '<li class="'.($current=='more')?'current':''.'"><em>More</em></li>';
echo '<li class="'.($current=='money')?'current':''.'"><em>Money</em></li>';
echo '<li class="'.($current=='account')?'current':''.'"><em>Account</em></li>';
echo '<li class="'.($current=='mail')?'current':''.'"><em>Mail</em></li>';
Looking at the code, I also see the end result is to assign on <li> element a class attribute value. JavaScript will do this better than PHP.
So you could give each <li> an id and leave the assignment of the class attribute to JavaScript:
echo '<li id="home"><em>Home</em></li>';
echo '<li id="users"><em>Users</em></li>';
echo '<li id="forum"><em>Forum</em></li>';
echo '<li id="more"><em>More</em></li>';
echo '<li id="money"><em>Money</em></li>';
echo '<li id="account"><em>Account</em></li>';
echo '<li id="mail"><em>Mail</em></li>';
echo '<script type="text/javascript">';
echo 'document.getElementById("'.$current.'").className = "current";';
// you'll want to sanitize $current to avoid parse errors in your JS
echo '</script>'
Use a switch instead of extensive if/else for starters :-)
// set variables if there array value exist
if (isset($header_home[$page_name])){
$current_home = 'current';
}else if (isset($header_users[$page_name])){
$current_users = 'current';
}else if (isset($header_forum[$page_name])){
$current_forum = 'current';
}else if (isset($header_more[$page_name])){
$current_more = 'current';
}else if (isset($header_money[$page_name])){
$current_money = 'current';
}else if (isset($header_account[$page_name])){
$current_account = 'current';
}else if (isset($header_mail[$page_name])){
$current_mail = 'current';
}
You can use variables variable ( http://www.php.net/manual/en/language.variables.variable.php ), foreach and break to reduce this part of function
I don't see any compelling reason to change your original code. It's very easy to read and understand, and easy enough to modify. I also don't think there's any reason to use Javascript to set the tab indicator. By using PHP, you cater to people who have javascript disabled, and I like to save Javascript for when it's truly needed.
If not changing to "return htmlcode" from "printing htmlcode", i would at least add a second parameter for that.

Categories