function to execute background process only once and refresh the page

I have a link page that is only available when the user login to the system,  I want to execute a php function only once if the user have not press that link.

If the link is pressed the background process generates a key and insert some values in a database then refresh the page and let the user view the results.

Scenario: a link have not been click
User login in the system;
- there is a list of links
- a link is available it says Run Me (the user have not pressed the link before)
- after pressing the link the page is refresh and the link is now label View Me

Scenario: a link was clicked in a previous session
User login in the system
-there is a list of links
- the link the user pressed in a previous session display View Me

What I would like is the PHP code to make sure when a specific link is pressed once, the option to Run Me is not longer available the user only can see the View Me label.

The goal is to toogle between the options of Run Me and View Me, making sure Run Me is only available if the user never clicked that link before. The link has id available and other values from the database.

thanks for your help!
LVL 8
a0k0a7Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PrograministratorConnect With a Mentor Commented:
Ooh, I understand now what you want to do,

you're right, javascript not enough,

OK, you need something like this :

<?php
if(isset($_GET['visited'])) {
$sql="update tbl set visited=1 where id=$_SESSION[id]";
mysql_query($sql);
echo "<script>window.location=''</script>";//place here the path that you want to get user to
}
else {
$res=mysql_query("select visited from tbl where id=$_SESSION[id]");
$result=mysql_fetch_array($res);
}
?>
<a href="?visited=1"><?php if($result['visited']==1) { echo "View Me"; } else { echo "Run Me"; } ?></a>

Open in new window


you need to add visited column to user table in your database.

Is that what you want?
0
 
PrograministratorCommented:
I think there is no need to using php here,

css and javascript will do that very will,

like this(for example) :
<html>
<head>
<style type="text/css">
a { color: #000; }
a:visited { color: #999; }
</style>
<script type="text/javascript">
function chklink() {
if(document.getElementById("lnk").style.color == "#999") {
document.getElementById("lnk").innerHTML = "View Me";
} else {
document.getElementById("lnk").innerHTML = "Run Me";
}
</script>
</head>
<body onLoad="chklink()">
<a href="" id="lnk">Run Me</a>
</body>
</html>

Open in new window

0
 
PrograministratorCommented:
sorry for typos :)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
a0k0a7Author Commented:
I think I need to be PHP and a database to make sure this link was not clicked prior from a different computer or another browser session.

I am not sure if Javascript code cover that scenario, do you think it will?
0
 
boraganeshCommented:
when you press the link first time make the log it into databse with all the information regarding browser client. when next time chekc it into DB.

or save the cookie on first time click and chekc that cookie is present or not
0
 
a0k0a7Author Commented:
@boraganesh the end user can click the link from several machines or a mobile device.

Could you please provide code or pseudo code to make sure the condition of a specific link click only  once is captured?
0
 
boraganeshCommented:
ok create table as follows

table name link_status
userid  link1 link2, link3, link4
1            1        0       0     0
2            1         1        1    0
3            1         1        1    0
4            1         1        1    0
5           0         0       0    0

when you are creating new user add entry for this user in above tabel with all zeros(meas not licked)
when he clicks on link make it as 1.

use tthis table to verify user has clicked partyicular link or not
0
 
a0k0a7Author Commented:
@Prograministrator

What does this line does... could you please describe it a little bit,

if(isset($_GET['visited'])) {


In the case I have several links.. does that still works?

Thank you so much for your help, it help me organize my design a lot...
0
 
Shinesh PremrajanTechnical ManagerCommented:
if(isset($_GET['visited'])) {

This means the variable "visited" is passed as get method to the script and the the value in the "visited" is not blank.

Hopethis helps
0
All Courses

From novice to tech pro — start learning today.