I am having an issue after I unset a cookie in php. This is my code
controller.php
setcookie("alert",'String',time()+3600,'/');
header("Location: ../home.php");
home.php
if(!empty($_COOKIE['alert']) && $_COOKIE['alert'] != ''){
malert($_COOKIE['alert']);
$_COOKIE['alert'] = '';
setcookie('alert', '', time()-1000);
setcookie('alert', '', time()-1000, '/');
print_r($_COOKIE);
}
When the home page loads the function malert works. But I expect it to skip the if condition when the page is refreshed again.. The problem is even after refreshing the home.php again , it is entering the if condition. Am i missing any information here on page refresh about cookie.?
The print_r output is
Array ( [PHPSESSID] => xxx [alert] => String )
Array ( [PHPSESSID] => xxx [alert] => )
Note: As I am using .htpassword in this folder I am not able to use SESSION
You're calling setcookie() after print_r(). You can't call setcookie() after outputting content because headers are already sent.
just try setcookie('alert', '', 1);
Related
I don't want to allow getting cookie from other page ! I have been searched on internet but not really found or may be I don't know how to mention that case .How can I manage that ! I want to get null in test2.php but get cookie in test.php ?
test.php
<?php
setcookie("acc_id", "23A", time() + 3600, '/');
header("test.php");
var_dump($_COOKIE); // 'acc_id' => string '23A' (length=3)
?>
test2.php
<?php
var_dump($_COOKIE); // 'acc_id' => string '23A' (length=3)
You can use the $path parameter in the setcookie syntax.
setcookie("acc_id", "23A", time() + 3600, '/test.php');
Now if you try print_r($_COOKIE['acc_id']); from your test2.php, it will show you Undefined Index, which means the cookie is not set for that page.
I have the following PHP script:
foreach( $_COOKIE as $key => $value ) {
if( strpos( $key, 'ticketRecon_ID-' ) === false ) continue;
else {
setcookie( $key, '', time() - 1 );
unset( $_COOKIE[$key] );
}
}
When I run the script, print_r($_COOKIE) shows me the targeted cookies have been deleted and do not exist. Yet the Chrome dev tool inspector shows the cookies are still present (see screen shot below) And when I return to the page where the cookies where created they are still present.
How do you completely delete/erase/destroy a _COOKIE in PHP? Might this have any bearing or relation to the site pages using the SSL protocol?
AMENDED:
Here is how I initially set the cookie:
setcookie( 'ticketRecon_ID-' . $row['reservationID'], 'N', null, '/' );
Solved it.
It seems you must delete a _COOKIE the same exact and identical way you create them. So I was missing the path attribute on my delete setcookie().
Created like this setcookie( 'ticketRecon_ID-' . $row['reservationID'], 'N', null, '/' );
So delete must be like this setcookie( $key, '', time() - 1, '/' );
Hope this post helps someone someday.
Assign it an empty array
$_COOKIE = array();
In my project, I'm trying to access the session data from 2 files, located in 2 different directories:
/site/page.extension.php <-- initializes the session and writes data to it
- also sets a cookie with session_id() and session_name()
/extension/ajax_handler.php <-- tries to access the session data, session_id()
- and session_name() are set via cookie and return the correct values
Now, my problem is, that even though session_id() and session_name() are the same in both files, I cannot access the session-array, it just returns an empty array.
My code:
page.extension.php:
session_start();
setcookie("psc_session", session_id(), strtotime("+20 minutes"), "/");
setcookie("psc_session_name", base64_encode(session_name()), strtotime("+20 minutes"), "/");
$_SESSION['uid'] = system::current_user_id();
ajax_handler.php:
session_id($_COOKIE['psc_session']);
session_name(base64_decode($_COOKIE['psc_session_name']));
session_start();
print_r($_SESSION); // => array(0) { }
I would really appreciate any help!
Greetings!
Update:
I've tried setting the session cookie params using this in page.extension.php:
$url = str_replace("http://", '', current_url(false)); // returns the current domain
session_set_cookie_params(10800, "/", $url, 0, 1);
If I now access session_get_cookie_params I receive (in ajax_handler.php):
print_r(session_get_cookie_params()); // =>
Array
(
[lifetime] => 0
[path] => /
[domain] =>
[secure] =>
[httponly] =>
)
Why does this happen?
I cannot replicate your problem, recreating the code you supplied the session variables and the cookies remain intact and are accessible from the ajax_handler.php. I'd suggest you backtrack and make sure both files are requested from the same domain.
The cookie which i set in codeigniter gets deleted after i restart the browser. I'm setting up a cookie like:
$test_cookie = array(
'name'=>'test',
'value'=> 'test',
'expire'=> time() + 60*60*24*14
);
$this->input->set_cookie($test_cookie);
The print_r($test_cookie) returns:
Array ( [name] => test [value] => test [expire] => 1309943188 )
Now i can print the cookie to make sure that the cookie is set:
$test_cookie= $this->input->cookie('test');
echo "<b> Cookie value: </b>". $test_cookie;
The cookie prints the value correctly.
However, if i restart the browser, i don't get the cookie value anymore. I've tried multiple browsers. With the var_dump, i get: bool(false)
Why the cookie is getting deleted when browser restarts?
Thanks.
The CodeIgniter documentation says the expires value is added to the current time. So effectively the expires value in your case is time() + time() + 60*60*24*14. This may be beyond the 32 Bit integer limit and turn into a negative value. This in turn will result in a temporary cookie that's deleted upon closing the browser.
$test_cookie = array(
'name'=>'test',
'value'=> 'test',
'expire'=> 60*60*24*14
);
should work. I think.
Hi is it possible to get embedded session id from url using php?
From the root url, http://www.sbstransit.com.sg/mobileiris/, the website will generate a session id which is between the url and become something like that.
i.e http://www.sbstransit.com.sg/mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx.
Isit possible to use php/any other ways to retrieve "ts2k1e55xaah50iwodsvjy35" out by querying the root url without actually physically going into the url?
If you use wget to get that page, you'll see:
...
HTTP request sent, awaiting response... 302 Found
Location: http://www.sbstransit.com.sg/mobileiris/(xidluk550vzs5045l1cxkh55)/index.aspx [following]
Which indicates that it is doing a 302 redirect to the URL containing the ID.
You can write Perl (or other) code to find the redirected URL:
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->requests_redirectable([]); # don't follow any redirects
my $response = $ua->get('http://www.sbstransit.com.sg/mobileiris/');
my $loc = $response->header('Location');
print "redirected to=$loc\n";
<?php
$url = 'http://www.sbstransit.com.sg/mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx';
$url_arr = parse_url($url);
print_r($url_arr); // debug output
$tokens = explode('/', $url_arr['path']);
print_r($tokens); // debug output
?>
Output:
Array
(
[scheme] => http
[host] => www.sbstransit.com.sg
[path] => /mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx
)
Array
(
[0] =>
[1] => mobileiris
[2] => (ts2k1e55xaah50iwodsvjy35)
[3] => index.aspx
)
So you could get your session id with $tokens[2]