Prevent Facemash cheating through url - php

I've been using the Facemash-like script. But the problem is that while rating people when we actually point our cursor towards a picture for every image there is a URL like:
rate.php?winner=XXX&loser=XXXX1
So, if we directly type this in the address bar the trick works! Hence there is chance for users to hack for their scores. I know we can change the GET methods to POST methods. And I've searched for this and nothing really helped me out. The links to the files(rate.php and index.php) are also included in the comments of this question.

I'm making my own Facemash-like engine and here's what I do.
I store two challengers' ids in PHP $_SESSION. Before displaying the new pair I check if $_SESSION is set and if it is I just display them instead of taking new pair from a database. This prevents cheating by refreshing the page until you get your photo. I did it because the community I'm making facemash for is relatively small.
So links look like vote.php?v=left or right. In vote.php I get ids from a $_SESSION['right'] and $_SESSION['left'] and then unset them. I looking forward to publish my script some day.

Yes, if you change from GET to POST then the parameters will not be displayed in the URL upon submission.
http://www.w3schools.com/php/php_post.asp

Instead of relying on GET/POST to determine the comparison, store the data in $_SESSION instead, and only let the user pick 'image1' or 'image2', then invalidate and create a new comparison after a choice is made.
Example site - form only lets you choose 1 or 2

Related

Use a anchor tag as a post method?

So, I'm trying to make a template type page that when a user clicks a product link, it takes them to the page and the php script auto-fills the page with the product info in the placeholders. The problem I'm having is I don't know how to make the product picture a link to the template page AND carry over a post method to let the php script know which product data to pull from the prelaoded array that I filled with product data from the php sql query. Any ideas?
Since you're mentioning anchors, that is a bad idea. They are usually encoded in the URL behind the hash sign (#) and not sent to the server.
So, one way to do it, is to either include the relevant data in the form content, e.g. in a hidden field.
Or another way would be to append it as a query parameter to the URL.
Hope that helps, and not taking security into account (e.g. both methods make it easy to perform SQL injection, or the parameters could be used to display something you don't want the user to select because it's out of stock, etc. pp.)!
since you're mentioning anchors with post, in my opinion this is not possible but one way to do it, is that using jquery and ajax.
see below link:
you can find the answer here

Alternatives for sending a parameter

I know two ways for sending an ID to another page:
as URI segment. exemple: http://mypage.com/index.php/ID
as an input field in a form using POST or GET methods.
Is there another way than these tow?
The purpose is to have a list of records. When the user clicks on one of them, s/he gets the full details of the selected record in a different page.
"Is there another way than these two?"
You can store it in $_SESSION too. If that what you are curious about.
But it's not the best way with your current problem.
Simply use GET (www.yourpage.com/records/THEID)
Well, you might consider this to fall under your first option of a URL segment, but "URL segment" usually means some part of the URL before any GET parameters...just to make sure you're aware of this, you can also put in GET parameters in a URL yourself without creating a form.
These days, URI segments like index.php/123 are popular, but traditionally the way to link to a detail page like that was index.php?id=123; then you can access the ID using $_GET['id'].

Persistent URL query string throughout the entire site?

This is one of those situations where I feel like there is a simple answer right in front of me...hopefully you guys/gals can show me the light.
PROBLEM :
I have a client that wants to maintain a query string across all pages on their site, only if they arrived at the site via a link that contains the query string.
This query string would also need to be passed with any external link from the site.
INFORMATION :
An example of the query string :
?utm_medium=<ad_placement>&utm_source=<ad_source>
(Obviously this is for tracking conversions from ads)
We're currently tracking all this stuff via Google Analytics (so, yes, I know that's an alternative), this is for an extra layer of reporting.
I did not code their site, which brings it's own set of issues (example: I'm not even sure they use a common header among all pages) - so I'm hoping there is a KISS answer out there for this.
Any suggestions?
If you just want to persist this exact query string on each page, why not set the linked pages to insert the string into a session variable. Then on each page link check to see if the session variable exists and add it to the query string when redirecting.
So - the answer here was this :
Use JS to grab the query string
Validate the query string
Add to a cookie
Append the cookie'd query string to every href in the DOM
Like I said...I knew there was a simple example staring me in the face haha!
This question is old and obviously must have been solved by now by #aepearson.
Adding another answer from current day perspective.
Now, one can also easily use sessionStorage also to save navigation data into a key. Pick it up whenever required, and add to url.
Example from Mozilla
// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');
// Remove saved data from sessionStorage
sessionStorage.removeItem('key');
// Remove all saved data from sessionStorage
sessionStorage.clear();

What technology use to make a webpage to update in real time?

I want to make a webpage where an user can add the title from a book he has read. These changes are reflected in real time on a list that contains all books he has introduced on the database, without the need to press any "reload" button. By example: there is no need to refresh (F5) the page to see the last book added.
I don't know if I can do this in PHP or in any other language, so I would like to know which is the best suited for something like this.
Thank you.
I think you are looking for Ajax. Would be able to asynchronously update the section of the page (the post in this case) without the need for page refresh.
You will want to do this with javascript, using the onchange event, and for a discussion on this you can look at: Call Javascript onchange event by programatically changing textbox value.
Basically, you react to the data being changed, then just send it immediately over using ajax to the server, but, you need to be aware of two things.
First, how will you handle errors, such as there is no book with that title, or the length is too long. I tend to put the error message in or by the place where they had the bad data.
The other is that you need to pass back the id when the data was inserted, so that when they change it again you can just do an update, so you will need to store that. I tend to put the database id I need in the element id, but you can keep it in an array in javascript, since it will maintain state for you.

Holding a URL parameter between pages?

I want to store a variable in the URL while they are browsing.
For example:
A menu, when the user selects ?category=shopping it goes to a map with shopping and they can click on a place and it should go to ?category=shop&id=22.
If they return to the menu then the ?category should be removed and if they click on something else e.g ?category=cafe.
I've been really puzzled with this and would appreciate any help - thanks!
If you just need to store state between pages, as your title suggests, then you can store this information inside the $_SESSION superglobal array. You start a new session by running session_start() as the very first line of any new page, before any output is sent to the browser. Anything you then store inside of $_SESSION will be available when you start the session in the same way on the next page.
If you're only interested in building a query string (i.e. the ?field=value&field2=value2 portion of the URL), as the content of your question indicates, then you might want to take a look at the http_build_query() function.
Your question seems a little ambiguous to me as to what your actual goal is for this, so I gave you both approaches. Just remember that you should use $_SESSION for state, and http_build_query() for creating dynamic URLs to point to specific content. Also remember that if the data needs to be secure, then you shouldn't put it in the URL or anywhere else the user could modify it, or where others could read it (e.g. in the browsers address bar). That sort of information needs to be in $_SESSION.
Thats a good use for session variables.
$_SESSION["category"]="stuff";
you can then keep it until you dont want it any more, or they terminate their session
I want to store a variable in the URL while they are browsing.
You can't actually "store" anything in the URL.
If you want to pass some data from one page to another using query string, you have to add this data to the query string.
"A map with shopping" should add category to the every it's link.
That's the way every web application works.
Session is not the way to go, because every page on the site should have it's address, and your category being important part of this address. If you store it in the session, no bookmark can be added, no link to be sent to a friend and no search engine will index your goods.

Categories