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.
Related
i have a list of page order by id .Example:
example.com/a?id=4
example.com/a?id=3
example.com/a?id=2
In every page a have string {order_id} Now i want first time load page it replace {order_id} with the id in url , from the second time it replace with empty string
in my code , it only work when i refresh the page with the same url ( example.com/a?id=4 and reload it ) but go to ( example.com/a?id=3 then go back example.com/a?id=4 it's not work) . Here is my code :
$id=$_POST['id'];
if(isset($_SESSION['orderid']) && $_SESSION['orderid'] == $id){
$this->mOutPut = str_replace('{order_id}',"", $this->mOutPut);
}else{
$this->mOutPut = str_replace('{order_id}',$id, $this->mOutPut);
}
session_start();
$_SESSION['orderid'] = $id;
Try:
session_start();
$id=$_POST['id'];
if(isset($_SESSION['orderid']) && $_SESSION['orderid'] == $id){
$this->mOutPut = str_replace('{order_id}',"", $this->mOutPut);
}else{
$this->mOutPut = str_replace('{order_id}',$id, $this->mOutPut);
}
$_SESSION['orderid'] = $id;
I don't seem to able to acquire a GET variable that is attached to the URI.
The codes are => at the controller
...
parse_str($_SERVER['QUERY_STRING'], $_GET);
....
$data['ti'] = $this->input->get('hamleno');
this->load->view('anasayfa', $data);
The codes are => at the view the link is
...
<div class="row"><?php for ($b=0; $b<=(count($hml)-1); $b++) { ?><?php echo $hml[$b]." "; ?> <?php } ?></div>
The link is working. I have added the
$config['uri_protocol'] = "PATH_INFO";
to the config.php file.
However I am not able to get the $ti variable
if ($ti){
$t=$ti;
}else{
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) {
$t=$t+1;
if($t>($uz-1)){
$t=$uz-1;
}
} // Forward button was pressed;
if( $this->input->post('geri') ) {
$t=$t-1;
if($t<0){
$t=0;
}
} // Back button was pressed;
}
I'm not familiar with codeigniter, but I've always passed GET variables in this manner:
URL = www.site.com/folder/webpage.php?myvariable=myvalue
I would retrieve that value this way:
$x = $_GET['myvariable'];
or with codeigniter: (I think)
$x = $this->input->get('myvariable');
Tailored to your example, I would personally de-obfuscate your loop code just a little and instead of switching from PHP to HTML and back in one line, I would simply echo both from PHP like this:
(I also don't exactly understand the url you're using, so here is my approximate)
<?php
for ($b=0; $b<=(count($hml)-1); $b++)
{
echo '',$hml[$b],' ';
}
?>
I found out how Codeigniter solves the problem =>
$get = $this->uri->uri_to_assoc();
if(isset($get['hamleno'])){
$data['ti'] = $get['hamleno'];
}
This sends the $b assigned to $hamleno together with all the other stuff in $data.
Thank you all for your kind comments
When the condition of the first if statement is met, the cookie "c2" is also set. I'm really scratching my head here. Any ideas? FYI this is the only code that handles cookies on this page.
<?php
//set cookies on correct answers
if ( $_POST['answer-1'] == 'rob' ) :
$c1 = 'correct';
setcookie('c1', 'correct');
endif;
if ( $_POST['answer-2'] == 'blue' ) :
$c2 = 'correct';
setcookie('c2', 'correct');
endif;
//reset cookies
if ( !empty($_POST['reset']) ) :
$_COOKIE['c1'] = '';
$_COOKIE['c2'] = '';
endif;
$c1c = $_COOKIE['c1'];
$c2c = $_COOKIE['c2'];
?>
This:
//reset cookies
if ( !empty($_POST['reset']) ) :
$_COOKIE['c1'] = '';
$_COOKIE['c2'] = '';
endif;
will reset the global $_COOKIE array with these values. But a page reload will still populate that again, as the cookies are still valid and stored in the browser.
In order to also delete them from the browser, you need:
//reset cookies
if ( !empty($_POST['reset']) ) :
$_COOKIE['c1'] = '';
$_COOKIE['c2'] = '';
setcookie('c1', '', -3600);
setcookie('c2', '', -3600);
endif;
I am having a litte issue with my searchengine.
It outputs the searchresult just fine, but when I am clicking a pagination link it fails, of course because it no longer have the $_POST array. I am using this piece of code:
if(empty($_POST) == false) {
$res = $search->search_ads($_POST);
if($res == false) {
$view->setData('numres', 0);
} else {
$view->setData('numres',$res[2]);
$view->setData('adverts', $res[0]);
}
$app->view()->setData('paginate', $res[1]);
$app->render('search.tpl');
} else {
$app->render('404.tpl');
}
When I click on f.x. "Page 2" it will render the 404 template.
Is there a way I can keep the $_POST array and reuse it in the search_ads function?
"paginate" contains the HTML for the pagination
<li><a class=\"paginate\" href=\"$target?page=$next_page&ipp=$this->items_per_page\">«</a></li>":"<li><span class=\"inactive\" href=\"#\">«</span></li>
use sessions.
session_start(); //on the top of your php file.
...
if(!empty($_POST))
$_SESSION['post_data']= $_POST;
...
if(empty($_SESSION['post_data']) == false) {
$res = $search->search_ads($_SESSION['post_data']);
...
}
Store the post array in a variable and use that elsewhere?
$post_array = $_POST;
$res = $search->search_ads($post_array);
You want to check for $_GET rather than $_POST on subsequent pages, as that's the method your using to pass the data:
href=\"$target?page=$next_page&ipp=$this->items_per_page\"
Will provide you with a $_GET array containing key value pairs:
$_GET['page'] = value of $next_page
$_GET['ipp'] = value of $this->items_per_page
I want to set a cookie with the country name and city location but I am unable as we had to set cookies before anything. How can I set my variables to its value?
include_once('ip2locationlite.class.php');
$ipLite = new ip2location_lite;
$ipLite->setKey('d930e8b9b1a38e8f647a5f22cce63e18a414e99aaf329a9f38a6caf8f623ec31');
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
$values="";
if (!empty($locations) && is_array($locations)) {
echo $locations['ipAddress'];
echo $locations['countryName'];
echo $locations['regionName'];
echo $locations['cityName'];
$values="ipaddress : ".$locations['ipAddress']."<br>country : ".$locations['countryName']."<br>region : ".$locations['regionName']."<br> city :".$locations['cityName'];
}
if(!isset($_COOKIE['trakcer']))
{
setcookie('trakcer',$values);
}
\you are using wrong way to set cookie, try:
if (!empty($locations) && is_array($locations)) {
$values = serialize($location); //serialize you array
//then set cookie
if(!isset($_COOKIE['trakcer'])) {
setcookie("CookieName", $values, time()+3600); /* expires in 1 hour */
}
}
//and to get it
$array = unserialize($_COOKIE['CookieName']);
You cannot do any echo or print before setcookie. Cookie can be set when no other headers were set/send.
Read documentation for more information php.net/setcookie