Hi,
before I start trying my luck on this projekt, I'd like to figure out if there isn't an easier or more logic way.
Project:
I'm using Chronoforms for a quite simple contactform, but I'd like to allow non-registered users to keep there data -once entered- in a cookie, so they don't need to fill in their name and stuff again and again
Plan:
Using PHP or JS to let ChronoForms set a cookie.
Worries:
- I read PHP must set cookies even before the header, which would be quite hard to realize with the Joomla, right?!
- I'm using more than one form - can they all read&write the same cookie?
- Is there a good howto on cookies in general and on chronoforms&cookies in detail?
many thanks in advance,
mh schreiber
before I start trying my luck on this projekt, I'd like to figure out if there isn't an easier or more logic way.
Project:
I'm using Chronoforms for a quite simple contactform, but I'd like to allow non-registered users to keep there data -once entered- in a cookie, so they don't need to fill in their name and stuff again and again
Plan:
Using PHP or JS to let ChronoForms set a cookie.
Worries:
- I read PHP must set cookies even before the header, which would be quite hard to realize with the Joomla, right?!
- I'm using more than one form - can they all read&write the same cookie?
- Is there a good howto on cookies in general and on chronoforms&cookies in detail?
many thanks in advance,
mh schreiber
Hi mh schreiber,
I don't think that there is any great problem with this. I have some multi-part forms that use cookies to remember users between the parts and they work pretty smoothly.
Here's a snippet that both reads and writes a cookie
Bob
I don't think that there is any great problem with this. I have some multi-part forms that use cookies to remember users between the parts and they work pretty smoothly.
Here's a snippet that both reads and writes a cookie
<?php
// Check uidp cookie, if it's not found then create one
// uidp = Unique IDentifier Pxxx
$uidp = "";
if ( JRequest::getVar('uidp', '', 'cookie', 'string') ) {
$uidp = JRequest::getString('uidp', '', 'cookie');
} else {
srand( ( double ) microtime() * 10000 );
$uidp = "I" . substr( base64_encode( md5( rand() ) ), 0, 16 );
setcookie('uidp', $uidp);
}
?>
Bob
Hi Bob,
thank you very much for the fast anwer! Your snippet works fine, but I might need another word of explanation.
What your Snippet does is set an unique id, on which a users could be identified. Now how do I do the next step to adress the textfields and save the entered data in the same cookie?
regards,
mh schreiber
thank you very much for the fast anwer! Your snippet works fine, but I might need another word of explanation.
What your Snippet does is set an unique id, on which a users could be identified. Now how do I do the next step to adress the textfields and save the entered data in the same cookie?
regards,
mh schreiber
Hi mh.schreiber,
Use the same syntax but add more setcookie('name', 'value'); entries
Bob
Use the same syntax but add more setcookie('name', 'value'); entries
Bob
Hi Bob,
thanks again for the fast anwer!
Setting Cookies works fine (thanks to your help :-) ), but "reading" them seems to fail. The data stored in the cookie(s) won't show up in the form after a reload.
Might there just be a mistake in my syntax?
On Submit code - before sending email:
Within the form-HTML:
And the input-field
Am I blind or is there a serious mistake?
Thanks!
thanks again for the fast anwer!
Setting Cookies works fine (thanks to your help :-) ), but "reading" them seems to fail. The data stored in the cookie(s) won't show up in the form after a reload.
Might there just be a mistake in my syntax?
On Submit code - before sending email:
<?php
$uidp = "";
if ( JRequest::getVar('uidp', '', 'cookie', 'string') ) {
$uidp = JRequest::getString('uidp', '', 'cookie');
} else {
srand( ( double ) microtime() * 10000 );
$uidp = "I" . substr( base64_encode( md5( rand() ) ), 0, 16 );
setcookie('uidp', $uidp);
}
if($_REQUEST['form']['vorname']) {
setcookie("vorname", $_REQUEST['form']['vorname']);
}
?>
Within the form-HTML:
<?php
if($_REQUEST['form']['vorname']) {
setcookie("form[vorname]", $_REQUEST['form']['vorname']);
}
if($_COOKIE['form'])
$form = $_COOKIE['form'];
?>
And the input-field
<input type="text" name="form[vorname]" value="<? echo $form['vorname']; ?>" />
Am I blind or is there a serious mistake?
Thanks!
Hi,
maybe the cookie name cant have [ brackets ?
Max
maybe the cookie name cant have [ brackets ?
Max
Hi Max,
wouldn't that mean the cookie wouldn't even be set? Because that's the only working part so farπ
mh schreiber
wouldn't that mean the cookie wouldn't even be set? Because that's the only working part so farπ
mh schreiber
ok, try to debug it line by line, try to cut your code into pieces and print the variables data and compare..etc, it looks a wasting of time but if you can't get through it ?π
Regards
Max
Regards
Max
Hi mh.schreiber,
Here's a slightly simplified version that works OK. Form HTML
I removed the arrays - they don't seem to work in cookies and they created some PHP Warnings in ChronoForms. If you need to do a bunch of these you can do them using a for each loop, possibly packing the results array into a single cookie if you need to.
Bob
Here's a slightly simplified version that works OK. Form HTML
<?php
if( isset($_REQUEST['vorname'])) {
setcookie("vorname", $_REQUEST['vorname']);
}
if( isset($_COOKIE['vorname']) ){
$vorname = $_COOKIE['vorname'];
} else {
$vorname = "Nicht gefunden";
}
?>
<input type="text" name="vorname" value="<?php echo $vorname; ?>" />
<br />
<input type="submit" name="submit" value="Submit" />
On Submit<?php
if ($_REQUEST['vorname'] ) {
setcookie("vorname", $_REQUEST['vorname']);
}
?>
I removed the arrays - they don't seem to work in cookies and they created some PHP Warnings in ChronoForms. If you need to do a bunch of these you can do them using a for each loop, possibly packing the results array into a single cookie if you need to.
Bob
Been a while since I played around with cookies and php, but as I recall, the only implication of having [] in the name, is that php will try and parse it as an array. As such, your code would seem proper.
Now, looking at your code, it appears to me, that you are setting the cookie 'vorname', but attempt to read the cookie 'form' (doesn't exist), and index 'vorname' of that.
Also, I'm wondering whether you really should be setting the cookie when the form loads, as there should be no input from the user there.
Solution: set and read the same cookie.
Edit: See you beat me to it again, Bob :wink:
Edit2: Have you put any thought of using sessions (JSession) with custom sid's?
Now, looking at your code, it appears to me, that you are setting the cookie 'vorname', but attempt to read the cookie 'form' (doesn't exist), and index 'vorname' of that.
Also, I'm wondering whether you really should be setting the cookie when the form loads, as there should be no input from the user there.
Solution: set and read the same cookie.
Edit: See you beat me to it again, Bob :wink:
Edit2: Have you put any thought of using sessions (JSession) with custom sid's?
Have you put any thought of using sessions (JSession) with custom sid's?
I prefer sessions for doing this, but sometimes cookies are needed, if cookies are not necessary here then sessions are definitely better!π
Max
Hi,
I thought the idea here was to give the cookie quite a long life so that if the user returned (a few weeks later maybe) their data would be available. I don't know much about sessions but don't they have a pretty short life?
I've handled this in the past by saving the data in the database but putting a unique id into a cookie, and cross-checking the email in case they use another computer. That's simpler than saving all the data in the cookie.
Bob
I thought the idea here was to give the cookie quite a long life so that if the user returned (a few weeks later maybe) their data would be available. I don't know much about sessions but don't they have a pretty short life?
I've handled this in the past by saving the data in the database but putting a unique id into a cookie, and cross-checking the email in case they use another computer. That's simpler than saving all the data in the cookie.
Bob
With php-sessions, the lifetime of the session is entirely up to the coder, and the sid is usually stored within a cookie (unless transparent sid is enabled).
I'm not sure how joomla would respond to creating custom JSession objects rather than retrieving the existing one from JFactory::getSession(), as the expire time setting may only be defined when the session object is created. So I guess this would be a dead-end for long-time session data.
I suppose one could create a psuedo-session class for storage, mimicking the behaviour of joomla/php sessions.
I'm not sure how joomla would respond to creating custom JSession objects rather than retrieving the existing one from JFactory::getSession(), as the expire time setting may only be defined when the session object is created. So I guess this would be a dead-end for long-time session data.
I suppose one could create a psuedo-session class for storage, mimicking the behaviour of joomla/php sessions.
Hi,
yes, I think for long time and non sensitive apps, cookies are preferred!
I think there is a problem here with the 2 dimensional cookie/request variables, it needs some debugging!
Regards,
Max
yes, I think for long time and non sensitive apps, cookies are preferred!
I think there is a problem here with the 2 dimensional cookie/request variables, it needs some debugging!
Regards,
Max
I've been playing with cookies to refill textboxes. This works fine. How can I do the same with textareas?
Hi tonny,
I imagine in the same way as an input field except that the value goes between the tags
Bob
I imagine in the same way as an input field except that the value goes between the tags
<textarea . . . >Some value here</textarea>
Bob
This topic is locked and no more replies can be posted.