methano Posted June 22, 2009 Share Posted June 22, 2009 I know I'll probably get hammered for not understanding the header problem, but is there a simple way to get data from a MySQL search or from a session variable, into a client cookie? The client cookie already has data in it and I want to change it. I'd like to put the code on an include page, which is where I seem to get into trouble. I've tried this with PHP code but would also consider using Javascript to read the session cookie, which also seems to be problematic. Link to comment https://forums.phpfreaks.com/topic/163242-solved-moving-session-cookie-data-to-client-cookie/ Share on other sites More sharing options...
rhodesa Posted June 22, 2009 Share Posted June 22, 2009 PHP Sessions can only be read by PHP, client side code won't be able to access them directly. You can use Cookies instead which are accessible by PHP and JavaScript Link to comment https://forums.phpfreaks.com/topic/163242-solved-moving-session-cookie-data-to-client-cookie/#findComment-861251 Share on other sites More sharing options...
methano Posted June 22, 2009 Author Share Posted June 22, 2009 Unfortunately, the data is coming from MySQL via PHP. I'm trying to embed the javascript instructions to set the client side cookie within PHP and it doesn't seem to work. I use: <?php $wholerxn = "smith"; echo "<script>function setCookie(\"wholerxn\",".$wholerxn.",5);</script>\n"; ?> where the javascript function is function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); } I've probably messes up the syntax. Link to comment https://forums.phpfreaks.com/topic/163242-solved-moving-session-cookie-data-to-client-cookie/#findComment-861316 Share on other sites More sharing options...
methano Posted June 22, 2009 Author Share Posted June 22, 2009 I finally got the syntax: echo "<script>setCookie('wholerxn','$wholerxn',5);</script>\n"; where: function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); } Thought I'd tried that already. Link to comment https://forums.phpfreaks.com/topic/163242-solved-moving-session-cookie-data-to-client-cookie/#findComment-861349 Share on other sites More sharing options...
rhodesa Posted June 22, 2009 Share Posted June 22, 2009 well...if the value is in PHP, you can just use PHP to set it: http://us2.php.net/setcookie Link to comment https://forums.phpfreaks.com/topic/163242-solved-moving-session-cookie-data-to-client-cookie/#findComment-861375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.