Laravel-Visitors Counter - php

this code repeats one IP hits every time when I visit this IP...
if I visit repeatedly with one IP hits increment, not IP increment
how can I solve this ?? can anyone guide?
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$value = date_create($get_visit_day->created_at);
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}else{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])
->increment('hits')->insert('updated_at', $date);
// DB::table('visitors')->insert('updated_at', $date);
}
// $value = date_create($get_visit_day->created_at);
if ($check_if_exists && date_format($value, 'd') != date('d')) {
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}[enter image description here][1]

try this if you want to add new record for each day , and increment view count for every hit for today form unique ip
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$is_today_if_exists = DB::table('visitors')->whereDate('created_at',$date)
->where('ip', $_SERVER['REMOTE_ADDR'])->count();
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}
elseif ($is_today_if_exists>0)
{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])->whereDate('created_at',$date)
->increment('hits');
}
else
{
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}

Related

Wordpress search through meta_value thats an array

If i use an array for a meta value can i see if a value is in the array when querying it? I have a website that has events that has dates attached to it as a meta value and i need to see if an event is on a certain date through a search.
$dates[] = 05/02/2016
$dates[] = 06/02/2016
$dates[] = 06/02/2016
update_post_meta($event, 'show_dates', $dates);
if i add this to an event how could i check if the 'show_dates' contains a date searched for? below is what i have tried already
$wp_query->set('post_status', array('publish', 'future'));
$wp_query->set("meta_key", "show_dates");
$wp_query->set("orderby", "meta_value");
$wp_query->set("order", "ASC");
$startDate = parseDatePicker($_GET['StartDate'], new \DateTime());
if (!is_null($startDate)) {
$wp_query->set("meta_query", array(
array(
'key' => "show_dates",
'value' => $startDate->format("d/m/Y"),
'compare' => 'IN'
)
));
}
Ok so it turns out the answer to this was easier than i expected. As Wordpress serializes the data in the array you can use LIKE instead of IN which will check the serialized array to see if it contains that date.
$wp_query->set('post_status', array('publish', 'future'));
$wp_query->set("meta_key", "show_dates");
$wp_query->set("orderby", "meta_value");
$wp_query->set("order", "ASC");
$startDate = parseDatePicker($_GET['StartDate'], new \DateTime());
if (!is_null($startDate)) {
$wp_query->set("meta_query", array(
array(
'key' => "show_dates",
'value' => $startDate->format("d/m/Y"),
'compare' => 'LIKE'
)
));
}
my next problem was how to work with a range I managed to figure this out and have posted below incase nayone else has a similar problem
if (!is_null($startDate) && !is_null($endDate) && ($startDate->format('d/m/Y') != $endDate->format('d/m/Y'))) {
$wp_query->set("relation", "OR");
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($startDate, $interval, $endDate);
$dates[] = $startDate->format('d/m/Y');
foreach($period as $day){
$dates[] = array(
'key' => "next_showing_date",
'value' => $day->format('d/m/Y'),
'compare' => 'LIKE',
);
}
$wp_query->set("meta_query", $dates);
}

codeigniter mysql stop third consecutive insert

I have this codeigniter mysql code to create an appointment:
if ($this->form_validation->run() == TRUE)
{
$data = array(
'startTime' => $startTime ,
'endTime' => $endTime ,
'day' => $date ,
'contact_id' => $this->input->post('InputPatient')
);
$this->db->insert('rdv', $data);
echo 'Appointment Created Successfully.';
}
What I need is - for a specific time like 8pm, I should not have more than 2 appointment created. On third insert with same start time, I should give an error.
Thanks.
Before you do your insert you could do a select to see how many entries there are with the same start time. If there are less than 2 then you can proceed to do your insert.
$this->db->select('count(*) as count')
->from('rdv')
->where('startTime', $startTime)
->where('day', $date);
$query = $this->db->get();
$aResult = $query->row_array();
if($aResult['count'] < 2)
{
//Do your insert
}
Here you go. Before you insert, check if there are already 2 appointments for a specific time and date.
if ($this->form_validation->run() == TRUE)
{
$data = array(
'startTime' => $startTime ,
'endTime' => $endTime ,
'day' => $date ,
'contact_id' => $this->input->post('InputPatient')
);
$isAlreadyTwo = $this->db->from("rdv")
->where(array("startTime" => $startTime, "day" => $day))
->count_all_results() >= 2;
if($isAlreadyTwo)
{
echo "Error!!";
}
else
{
$this->db->insert('rdv', $data);
echo 'Appointment Created Successfully.';
}
}
Hope this helps :)

Warning: Header may not contain more than a single header, new line detected

I have a question about PHP error. I went through other similar questions, and apply it, but I still have a problem like this, so I am asking it.
Situation is like this. There is an text box called special instruction; if I put just one line it is fine to work, but if I put more than one line it causes problem like this. I put urlencode(str) to solve the problem, but it is still showed up. What do I need to change? T.T
error message is "Warning: Header may not contain more than a single header, new line detected."
if($_POST['Submit'] == 'Submit Order'){
$today = date('D M dY');
$to_time = date('h:i a');
$hours = substr($to_time, 0, -6);
$minutes = substr($to_time, -5, 2);
$seconds = substr($to_time, -2);
$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
$date123 = $_POST['day'];
$date_post = date('D M dY',$date123);
$time_post = $_POST['time'];
$hours_post = substr($time_post, 0, -6);
$minutes_post = substr($time_post, -5, 2);
$seconds_post = substr($time_post, -2);
$total_seconds_post = ($hours_post * 3600) + ($minutes_post * 60) + $seconds_post;
$tip_amo = $_POST["tip_amount"];
$deliv_mails = $_POST["deliv_mails"];
$coupon_no = $_POST['coupon_no'];
$minorder = $_POST['minorder'];
$subtotal_amount = $_POST['subtotal_amount'];
$payment_type = $_POST['payment_type'];
$comments = addslashes($_POST['comments']);
$coupon_code = $_REQUEST["coupon_code"];
$customer_comments = get_field(CART,"additional_text","session_id='$sessionid'");
if($comments == ''){
$comments1 = $customer_comments;
}else{
$comments1 = $comments;
}
if($time_post!='') {
if($today==$date_post){
$query = [
'day' => $date123,
'time' => $time_post,
'coupon_no' => $coupon_no,
'minorder' => $minorder,
'subtotal_amount' => $subtotal_amount,
'payment_type' => $payment_type,
'comments' => $comments1,
'coupon_code' => $coupon_code,
'tip_amount' => $tip_amo,
'deliv_mails' => $deliv_mails,
'd' => $address
];
header('Location: details_info_order.php?' . http_build_query($query));
exit;
}else{
$query = [
'day' => $date123,
'time' => $time_post,
'coupon_no' => $coupon_no,
'minorder' => $minorder,
'subtotal_amount' => $subtotal_amount,
'payment_type' => $payment_type,
'comments' => $comments1,
'coupon_code' => $coupon_code,
'tip_amount' => $tip_amo,
'deliv_mails' => $deliv_mails,
'd' => $address
];
header('Location: details_info_order.php?' . http_build_query($query));
exit;
}
}
}
Warning: Header may not contain more than a single header, new line detected. in /home4/mikehan2/public_html/mikehan7/final_checkout_block.php on line 64
it is the exact error message.
You should really use something like http_build_query(); no need to manually URL encode and no messy string concatenation, eg
$query = array(
'day' => $date123,
'time' => $time_post,
// etc
);
header('Location: details_info_order.php?' . http_build_query($query));
exit;
Some of your variables contains a multiline text, maybe COMMENTS.
Use urlencode:
'comments' => urlencode($comments1),
...

Getting endless loop in PHP

Whenever i try to run this code, it seems like it gets into an endless loop. But, i cant figure out whats causing this problem. Maybe an extra eye on the thing would be able do point out the problem?
The problem only accours when there is a different year zone, example 2012-2018
Example: $this->budget_model->set_monthly_budget('1','2012', '8','2014','1');
function set_monthly_budget($start_month, $start_year, $end_month, $end_year, $budget_group_id)
{
$user_id = 2;
// Current date
$current_month = $start_month;
$current_year = $start_year;
$days_in_current_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
$company_id = 1;
$month_goal = 100;
// Check if it is the current month
if($start_year == $end_year)
{
for($x=$current_month;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
return true; // Return true if the task was completed
}
if($start_year !== $end_year)
{
$temp_start_year = $start_year;
while($temp_start_year !== $end_year)
{
// Check if we are in the same year as we started
if($temp_start_year == $current_year)
{
// Insert remaining months for this year
for($x=$current_month;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if the temp and end year is the same
if($temp_start_year < $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if we are in the same year as we started
if($temp_start_year == $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
$temp_start_year++;
}
}
}
in your code
while($temp_start_year !== $end_year)
you used !== which also check if the type of the 2 variables are the same.
but this line
$temp_start_year++;
will implicitly cast the variable to integer.
Therefore the !== will be comparing integer to string, which will always evaluate to true.
The solution is as simple as using != instead of !==, or feed an integer instead of string when you call your function (remove the single quotes).

Logo change with date script

I just wondered if anybody can point me in the right direction: I'm looking to make a script whereby the logo on my site changes depending on the date; so for instance a haloween style one soon.
I started off by having 2 arrays, 1 of start dates and 1 of end dates(not sure even if this is the best way!):
<?php
$start_dates = array('01/01' => 'New Years',
'14/02' => 'Valentine Day',
'16/02/2010' => 'Pancake Day',
'17/03' => 'St Patricks Day',
'01/04' => 'April Fools',
'02/04/2010' => 'Easter',
'23/04' => 'St Georges Day',
'11/06/2010' => 'World Cup',
'31/10' => 'Halloween',
'05/11' => 'Guy Fawkes',
'11/11' => 'Armistice Day',
'16/10' => 'Today',
'15/12' => 'Christmas');
$end_dates = array( '08/01' => 'New Years',
'15/02' => 'Valentine Day',
'17/02/2010' => 'Pancake Day',
'18/03' => 'St Patricks Day',
'02/04' => 'April Fools',
'06/04/2010' => 'Easter',
'24/04' => 'St Georges Day',
'12/07/2010' => 'World Cup',
'01/11' => 'Halloween',
'06/11' => 'Guy Fawkes',
'12/11' => 'Armistice Day',
'17/10' => 'Today',
'01/01' => 'Christmas');
?>
Easy so far...the problemis that I need a way of working out if todays date falls between the start date and end date, then changing the image file name.
Its a long shot but I hope someone would be kind enough to help.
Thanks,
B.
like this
$events = array(
'New Year' => '01/01 01/08',
'Pancake Day' => '16/02/2010 17/02/2010',
//etc
);
echo find_event($events, '16/02');
where find_event() is
function mdy2time($date) {
$e = explode('/', $date);
if(count($e) < 3)
$e[] = '2010';
return strtotime("$e[1]-$e[0]-$e[2]");
}
function find_event($events, $date = null) {
$date = is_null($date) ? time() : mdy2time($date);
foreach($events as $name => $range) {
list($start, $end) = explode(' ', $range);
if($date >= mdy2time($start) && $date <= mdy2time($end))
return $name;
}
return null;
}
you should use an array more like this:
$dates = array();
$dates[] = array(
'name' => 'New Years'
'start' = '01/14',
'end' => '01/20',
'style' => 'haloween',
);
$dates[] = array(
//...
);
then you can get the style as follows:
$style='default';
// date as number e.g. 130 (january 30th)
$currDate = date('md',time()) * 1;
foreach ($dates AS $k => $v) {
$tmp = explode("/",$v['start'];
$start = ($tmp[1].$tmp[0])*1;
$tmp = explode("/",$v['end'];
$stop = ($tmp[1].$tmp[0])*1;
if ($start <= $currDate && $currDate < $stop) {
$style=$v['style'];
break;
}
}
echo 'style: '.$style;
Didn't check the code yet, so feel free to correct me if iam wrong.

Categories