Jean
Jean

Reputation: 5411

PHP get cookie value

I create a cookie with javascript with this code:

$('#solicita').click(function(){
  document.cookie = 'courseId=<?php echo $course[id]?>';
  $('#myModal').modal('show');
});

This create a cookie with the name courseId with a value. Then I'm trying to read the cookie with the following php code:

if(isset($_COOKIE['courseId'])) {
 $course   = ORM::for_table('courses')->find_one($_COOKIE['courseId']);
 ...
}

but isset($_COOKIE['courseId'] is always false. This code was working until last week that I change my .htaccess in order to have a friendly links. This is the code I added to my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteEngine On
RewriteBase /peppers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/artes-escenicas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/ciencia-y-tecnologia/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/arte-y-diseno/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/deportes/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/idiomas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/academico/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/rutas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/voluntariado/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]

Some help please

UPDATE


enter image description here enter image description here

Upvotes: 1

Views: 1550

Answers (1)

C3roe
C3roe

Reputation: 96250

Cookies are limited to a certain path, and will only be send again by the browser if the requested URL lies within that path.

You can see in your screenshot that your cookie has a path value of /peppers/cp/artes-escenicas. When setting a cookie via JavaScript, the path defaults to the path of the document that the script setting is cookie is running in.

If you want it to be send for other resources outside of that path as well, then you have to specify a less restrictive path when setting that cookie.

Upvotes: 1

Related Questions