disable href link on click

Is there a way to disable href link on click and enable after 5 days.

I have following code from http://coursesweb.net/javascript/ which work fine but by refreshing page href link is enable (active) again even when time in milliseconds is set for 5 days

Click - Click 2

The only way I can think of you doing this is using Ajax and PHP/MySQL, where PHP would keep track of the time that is stored in a database or you could use $_SESSION (cookies). Come to think of it, there is a way to use cookies in JavaScript (I used the JQuery library cookie pluginonce), where you wouldn’t need a server-side language.

If you use session cookies will it keep track if you open page in browser following day to still disable link until 5th day

Do you have any sample code to use cookies in java script to disable link on click and enable after 5 days

Johan, you should remember that Javascript is CLIENT-SIDE… Therefore easy to hack.
Anyone can look at your JS code by pulling the page’s source and still execute the disabled link.

It would be much better to just use your PHP side of the site. It is much more secure to NOT show any
links that are to be disabled. Unless you want to still allow users to be able to manually access the link.

Either way, you using the code you show, the page would have to be active for the five days. Going back
to it does not indicate a starting date. The starting date of the link would have to be stored in a hidden
field so the JS routine could compare it to the current date.

To recap… Loading a page into a browser restarts any JS code and therefore will restart your timer.
As Strider64 mentioned, you can use cookies which are sort-of-secure, but, you would need one for
each link that you wanted enabled or disabled. So, if these links are being pulled from a database as they
are displayed, you could simply run your query to pull only the ones that are active.

Perhaps a little more information on how you are creating your links and where they come from might
help us. Well, a few ideas to play with… Good luck.

Thanx, have been sorted with cookies - jquery.cookie.js

Cool ! Glad you sorted it out. Just remember cookies are CLIENT-SIDE and can be removed anytime
that the user does computer maintenance and some users have cookies turned off. So, for each user
you should check to see if cookies are turned off on their system. Just a thought…

Will mark this solved!

Didn’t thought about that is there a way to use php with no database
I have only one link and page is cookie password protected

Boost Your Post

Another problem if user brows direct to…/ post.php (also password protected but once login script runs again and post.php get execute again thus bypassing link disable

Well, there are many ways around that issue. First, you can use SESSION variables inside of your
login code. You set a SESSION variables such as $_SESSION[“user_logged_in”]=“yes”; which will
be passed around your site. In every page, the first PHP code would be to check this variable and
see if it is YES or not. If not, it would just redirect to another page that mentions the user has tried
to access a page they are not allowed to be in. (With a link to the log-in page of course.)

If there is just one link you can do that a lot of ways. But, if there were 20 links, cookies get tricky
as you need to track all the link names and check their dates. Etc…

Using PHP without a database, well, who would want to! LOL But, without it, you can use SERVER-SIDE
files to do this with ease. Just keep an array in PHP that is loaded from a file and save it back as needed.
Reading and writing files takes only a little code and can be used in place of CLIENT-SIDE cookies.

Lots of ways to do this.

Herewith code I am doing it with jquery.cookie.js

How can I adapt with what you are saying using SERVER-SIDE files. Keep an array in PHP that is loaded from a file and save it back as needed.

[code][/code]

Well, it is very easy. Remember, text file access on a server is not fast and should only be used for
smaller arrays. Here is the basic way it is done. Both writing it and reading it back in. Arrays are
stored as text, but, they have indexes and the such. So, to convert them you use the “serialize”
functions which translate them into a format that is writable…

[php]
$serializedData = serialize($array); // Where ‘$array’ is your array name
file_put_contents(‘your_file_name.txt’, $serializedData); // Writes the converted data to the file

// Read the data back in at a later point
$recoveredData = file_get_contents(‘your_file_name.txt’); // Read in the entire file…
$recoveredArray = unserialize($recoveredData); // Convert back to PHP array format

// you can print your array like or do whatever you want with it
print_r($recoveredArray);
[/php]
As you see, this process is so very simple and very easy to use. I did not bother to alter the example
to match your uses. This is just an example on how it is used. Basically, just two lines out and two lines
back in. Very simple. Should work good for you if the array is small.

A couple notes on it’s usage. Remember it is on the server, so the files are protected and are not on
any client’s system. If two users need different data, you will need to have multiple files or just add
another sub-index in the array. (Multidimensional arrays work this way, too.)

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service