/    Sign up×
Community /Pin to ProfileBookmark

What does this code do please

[CODE]function go() {
if (document.cookie.indexOf(‘H4_fly=’) >= 0) {
var d = new Date();
d.setTime(d.getTime() – 10000);
document.cookie = ‘H4_fly=0;expires=’ + d.toUTCString();
document.getElementById(‘flyForm’).submit();
} else {
history.back();
}
}[/CODE]

The <body onload=”go()”> clearly calls the go function as above, which submits the form ‘flyForm’ on page load – so good so far. What does the if statement determine, and maybe more importantly what is written to document.cookie if the if statement is executed ?

Reason for the question. Well on my local system in the UK everything works well, my php file populates the html form and it is sent on loading the page. My webhost is in Germany, and their server is set one hour ahead of the UK, and on transfering these files to that the behaviour has become somewhat erratic. It works first time fine, and it works if you clear cookies or delete private data on Firefox. Do I need to modify the – 10000 to take into account the time difference, and if so, to what.

All help very much appreciated.

Blu

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rootJun 13.2007 — What does your form do?

Having a cookie... having a form submitted by a cookie... Hmmm, why would you want to do this?
Copy linkTweet thisAlerts:
@blulagoonauthorJun 14.2007 — The form submits POST data using type=hidden input fields which are populated by the PHP script, all that side of things works fine.

I didn't write the code originally, but I think the idea is that once you've submitted the form you set a cookie which prevents you from sending the form again for a period of time, and instead you get a security violation alert.

For some reason using a server in a different time zone seems to cause problems and I wondered if anyone knew why that might be.
Copy linkTweet thisAlerts:
@rootJun 14.2007 — Far simple methods to stop the user from submitting more than once are available like disable the submit button on press... no need for cookies and TBH using cookies and having the form submit from the cookie routine raises alarm berlls as you have no idea what data is in the cookie nor do you have any idea if any other data has been collated and then sent allong with the form...

It is all suspect.

in the document form, find the <input tupe="Submit".... button and add the property, onClick="this.disabled=true;" and the button should become clickable only once.
Copy linkTweet thisAlerts:
@blulagoonauthorJun 14.2007 — I agree it certainly isn't the way I'd do it either, but as I say I didn't write the code.

in the document form, find the <input tupe="Submit".... button and add the property, onClick="this.disabled=true;" and the button should become clickable only once.[/QUOTE]

There isn't a submit object within the form though, form submission is handled by the <body onload="go()"> which calls the go function once that part of the script is reached, by which time the hidden fields have been populated by the PHP script.

More specifically what does this bit do ?

var d = new Date();

d.setTime(d.getTime() - 10000);

If d.getTime is 10am what would d.setTime be, less 10,000 seconds, thats what 7.13 and 33 seconds ?

d.toUTCString(); what does this function do ?

Thanks
Copy linkTweet thisAlerts:
@slaughtersJun 14.2007 — ...d.toUTCString(); what does this function do ?[/QUOTE]
http://www.w3schools.com/jsref/jsref_toUTCString.asp

Your code just checks to see if a cookie exists.

If it does then it expires the cookie by setting the expiration date to sometime in past then submits a form.

Pretty weird if you ask me.

P.S. Commenting the darn thing would not hurt.
×

Success!

Help @blulagoon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...