Limiting a user's action to happen only once [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to know if it is possible with PHP to make action only happen once.
I have a script where users click an image and gain points for clicking it, but I would like to make it so that if they click again it won't add points anymore for a certain amount of time, or an advertisement would disappear from the site for that particular user.
Is this possible?

Store it in the database.
When they click on the image, run a query that changes the is_clicked column (for instance) to clicked or 1 or whatever you like.
Retrieve the image in such a way that, if the is_clicked column is changed to clicked for a particular user, then don't allow any clicks.
For example :
if($row['is_clicked'] != 'clicked']){
echo '<img>....</img>';
}

It is possible but with PHP, it can get pretty messy. I would suggest you use JS (Javascript). Create a simple function in JS and then use the onclick attribute in the the button element to execute. Like this:-
<script>
document.getElementById('id of the button').onclick='myFunc';
function myfunc(){
document.getElementById('id of the button').style.display="block";
}
</script>
Front end, html:
<button id="id of the button">Click here!</button>
Now if you want to make sure a person who clicks button from a computer shouldn't be able to click it again, then you should set up a cookie using php which will retrieve data about its computer and prevent the computer from using this button

Related

how to generate sharable link based on form input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
So I'm working on a form that takes a link and outputs to a page with a custom made header by me.(similar to StumbleUpon)
What I'm having trouble with is being able to share that page on facebook or other social media. Because currently it has the same link no matter what page it is.(the link is the page I have the form action as).
I want the outputted page to be have "sharable" link. Like when I paste the link of that page on facebook, it generates thumbnails, a short description,etc.
Well, basically, the process should be something along the lines:
1. Take input from the form (presumabli input type=text name=url)
2. Save that $_POST[url] to the database, and assign unique identifier to it (id, hash, uuid, ...)
3. Redirect user to visit.php?hash=assigned_hash which will display the saved page based on the hash from the database
That is one approach (better in my opinion). Another approach is, change form method to GET, so you will get link
http://yoursite/url.php?url=http...
Which can then be copied and pasted, but also enables users to see the link and possibly alter it and what else.
Let me know what you think

How to refresh only a specific function in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a function to show current date and time using date() function in php.
I used
meta http-equiv="refresh" content="30" to refresh the whole page.
But i want to refresh only that specific function part as i want to change the time every minute. Is it possible?
In short, no it is not possible with just PHP and HTML.
Once your page has been generated by your PHP script, it is sent to the client and cannot be modified anymore. HTML is made to build "static" web pages, that mean they won't produce fancy moving things and therefore they won't update content once the page is loaded.
But even if you cannot send the page a second time once it has been generated by PHP, you're not bound to HTML in the page. You can set up script in the page that will for example make the client browser perform a request to your server to update part of the page. That's called AJAX, and to do it you have to learn Javascript.
You should use client-side magic for this, so I'd use AJAX techniques.
setInterval(function(){
$("#time").load("page.php #time");
}, 30000);
Justin E asks, you can actually load a specific element from a page. Yes you can.

How should I implement a "like" counter to my site? (not facebook) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my website users can post submissions, and I want other users to be able to do what would be my equivalent of facebook's "likes".
But I don't know how to implement this. My main problem is that I need to prevent uses from liking the same thing more than once.
I was planning to add a long string into the database entry of each submission, for example "userid:rating/userid:rating/userid:rating". But I think that would be highly inefficient, because I would need to parse the string every time someone presses a button, and if there's lots of ratings then it would have to do a lot of work I think.
Should I make a separate table for ratings globally, and use the submission ID to link them to the right thing, or what? I feel it would be very inefficient to make mysql dig through the whole database and look for all individual rating entries that have a matching ID every time someone opens a submission page...
You create another table: submission_rating. Three columns would suffice: rating, submission_id and user_id.
Someone presses like, you do an an INSERT. But, before you do the insert, you check whether or not this particular user has already liked this submission. If the user has, you remove the like. If it does not, you insert the rating.
edit: As two gentleman below suggested, rather than relying on another check, go for the UNIQUE index. Make sure you catch the error and properly show it when trying to insert. Boldly said, if(!$insert) {}.

How do I increment a variable in PHP when an href link is clicked [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want $counter=$counter +1, when this link on my website is clicked:
<img src="images/old.jpg" width="190" height="32" />
I've tried $counter++; in several creative ways, none of which seem to have worked.
PHP is a server-side language.
This means that PHP is (most often) only responsible from
When the server receives a request
until
The PHP program outputs the page.
So if you want something to happen in response to your action AFTER the page is loaded (e.g. clicking a link), PHP cannot handle that.
Now, you should choose your implementation in either Javascript or PHP depending on what you want to achieve.
If you want to store the counter value in your server, and increment it:
Set the destination of the link to the exactly same page as the one you are viewing now. Use $_SESSION to store a value, and it will be stored across multiple requests.
If you want to see the value of the counter go up as you click on the link:
Use Javascript to store the counter as an variable, and increment it on each click. This will be reset if you refresh the page.
Note: You cannot achieve neither of this if your link takes you to another page. That becomes a whole new story.

Advanced Session Script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question
I was wondering if anybody knows how to make a script that would essentially save a percentage of how much you completed of say a test or something and when you go back to continue it will bring you to the page you're at. For example theres 5 pages, and you're on page 3 so it'll save as 60% and whenever you click continue it brings you to that page.
When I say advanced I mean like secure so you can't go skip to page 5 just by putting the url. For example at the head of each test page I would put this
if (session > 60%) {
header(Location: page3);
}
Do you guys know what I mean?
I was hoping somebody could just help me do this somehow and make it so it saves under the users profile and you can call the percentage whenever using a variable. Any ideas?
You're pretty close. First, check out any PHP Session Tutrorial to learn about them a bit.
You can access session variables with $_SESSION['keyname'] (after you've called session_start() on your page.
Simply save your progress as one of these, e.g. $_SESSION['progress'] = 60;
and evaluate by testing if the session is set (so you don't get an error), and then evaluate it as follows:
if ( isset($_SESSION['progress']) )
{
//write your if (or switch) statement here
}
Let me know if that makes sense or if you have any questions :)

Categories