Use AJAX to reload captcha - php

This question may have been asked already - but unfortunately, I could not find any satisfactory answers. I will just ask it for my concrete case and ask the admins not to delete the question for at least a few days so I can try it out...
I have a page. It uses a captcha. Like so:
<?php
session_start(); // the captcha saves the md5 into the session
?>
<img src="captcha.php" onclick="this.src = this.src" />
That was my first code. It did not work, because the browser condsidered it useless to reload an image if the source is the same. My current solution is to pass a get parameter:
onclick="this.src = 'captcha.php?randomNumber='+ranNum"
The JavaScript variable var ranNum is generated randomly every time the onclick event fires. It works fine, still, I don't like the possibility, if the - though improbable - case of two numbers being the same twice in a row. Although the random number varies between -50,000 and 50,000 - I still do not like it. And I don't think the method is right. I would like to know the 'righter' method, by means of AJAX. I know it's possible. I hope you know how it's possible ^^ In that case, please show me.
Thanks in advance!
By the way - if I spell cap(t)cha differently, never mind, the reference to the PHP file is right in my code: I use randomImage.php
EDIT: The random number in JavaScript is only generated so the image reloads. Captcha.php does not care for the $_GET parameter. The string really is random.
EDIT: Thus, what I would like to know is how to make the browser relaod the image without passing different get parameters every time the event fires.

Unfortunately, AJAX doesn't provide a good way to dynamically load images. Even using the javascript "preload" trick just gets the browser to load each image once per URL. The only good way to get the browser to load another image from the same source is to use a different parameter just like you are doing now. And, like other answers have stated, timestamp should be sufficient for that.

Have you considered using a timestamp instead?
onclick="this.src='captcha.php?ts='+Math.round(new Date().getTime()/1000)"

Just use:
<img src="captcha.php" onclick='this.src = "captcha.php&time=" + new Date().getTime();' />
You can discard the time parameter and generate the random number in PHP. :)

You could also get the image from an Ajax request base64 encoded and put it into the img tag too.
Of course I think it is overkill and a base64 encoded file is about 4/3 of the original's size. (A 3 kb image would be about 4kb on base64).
Edit:
to have the img src attribute like
data:image/png;base64,base64encodedimage

Related

How I can maximize the Get query length?

I'm using Godaddy hosting plan & I create a php page to submit information to my lead buyer using get method. ode is working fine & when I click on submit data, it runs the query but didn't take the complete data with it. S I need to know how I can increase the query length so it takes my complete data with it. Here are few points that might help you understanding my problem.
I tried Chrome, Firefox. Results are same on both browser.
I can't use post method, because it's allowed to do so, So I need to post it using GET method in any way.
I'm attaching the PHP code so you can get the idea what i'm doing exactly.
echo "<td>Submit</td>";
I think you're missing quotes around your href. Try this. Note that you really, really, really, should use POST for this.
echo "<td>Submit</td>";

JQ-GRID implementation in php file

Hi friends i am working on JQ-GRID. I want to show Image in specific column, But i don't know how to attach image in JQ-GRID. Can anybody help me or please send me some links, thanks
jqgrid is a feature monster. I tell this everybody who asks about it.
When jqgrid loads, a function is called which actually gets the data you want to display.
This is normally an ajax call to your php. As a result set of this function, you can just use xml or json.
I prefer json, so I build my result array and do a echo json_encode($myarray)
jQuery("#your_grid_id").jqGrid({ url : '/ajax/getjqgriddata.php'})
Now displaying pictures, there are different ways you can do that. You can either generate a <img src="wherever/mypicture1.png"></img>-link and hand it over in your result, or encode your picture binary data with base64 and deliver it with your result.
A more addvanced way is to use an so called formatter and just returning a id for the image.
This depends on you, but I would suggest to get confident with jqgrid, experiment with returning -links to get a feeling how jqgrid works.
There is plenty of good documentation at:
http://trirand.com/blog/jqgrid/jqgrid.html
Just take a look at it.

Best way to save info in hash

I have a webpage that the user inputs data into a textarea and then process and display it with some javascript. For example if the user types:
_Hello_ *World* it would do something like:
<underline>Hello</underline> <b>World</b>
Or something like that, the details aren't important. Now the user can "save" the page to make it something like site.com/page#_Hello_%20*World* and share that link with others.
My question is: Is this the best way to do this? Is there a limit on a url that I should be worried about? Should I do something like what jsfiddle does?
I would prefer not to as the site would work offline if the full text would be in the hash, and as the nature of the site is to be used offline, the user would have to first cache the jsfiddle-like hash before they could use it.
What's the best way to do this?
EDIT: Ok the example I gave is nothing similar to what I'm actually doing. I'm not cloning markdown or using underline or b tags, just wanted to illustrate what I wanted
Instead of trying to save stuff in the URL, you should use the same approach that is common in pastebins: you store the data , can provide use with url, containing an unique string to identify stored document. Something like http://foo.bar/g4jg64
From URL you get state or identifiers, not the data.
URLs are typically limited to 2KB total, but there is no officially designated limit. It is browser-dependent.
Other than that, make sure you properly URL encode what you're putting up there, and you're fine... although I certainly would not want to deal with obnoxiously long URLs. I might suggest you also avoid tags such as <underline> and <b>, as they have been deprecated for a very, very long time.
Use javascript function:
encodeURIComponent('_Hello_ *World*');

retrieve a variable value of a div in a database

I try to retrieve a variable value of a div (height) in order to put it in a table of a database in real-time (I absolutely need it). The value of the height is reacting with the microphone of the device. I would scream in the mic of a computer and see the result on another computer. Is there a simple way to do it?
Thanks.
EDIT : Actually I bring back the sound informations with JRecorder (sajithmr.me/jrecorder-jquery). The height of the div is equivalent to the amplitude of the scream. I would put the value of this height in a simple table.
You'll need to throw in some JavaScript in there. I'll use jQuery in my examples.
To get the height of the div, you can use getClientRects.
var height = $("#yourdiv")[0].getClientRects()[0].height; //For IE, substract the bottom property from the top property
To send it to the server, you can use AJAX
$.post("yourpage.php", {height: height});
In your server PHP page, you can then retrieve the value and store it in the DB:
$height = $_POST['height'];
You now have the value in your database. For another client to see it, you need to make a page that display the latest value and poll that page with AJAX at a set interval to get the value and update your div.
There is no simple way to do this. This is the most simple I could think of. It still requires a good bit of knowledge of JavaScript and AJAX. If you have any questions leave a comment.
PHP can't do anything with your device (microphone), as it's server-side software, not client-side
I didn't get your first part of question, though.
Using jRecorder Plugin, you can use the callback function to decide the mic activity level.
callback_activityLevel : Callback for returning current mic input level (0-100)
You can check the example at: http://www.sajithmr.me/jrecorder/example1.html

Tracking Quiz Results with URL, No Database Allowed!

I need to create a 10 page quiz for a mobile browser. It is only a mobile webpage, so no considerations need to be taken for other browsers.
Here's the problem I'm having: I can't use JavaScript, because not every mobile browser supports it. I'm not very skilled in other languages, but I thought perhaps something could be done in PHP as it is server-side.
If my first URL is domain and I enter the correct quiz answer, the URL to the next page could be domain/?p=1. The URL doesn't need to do anything but hold a count of the number of correct results.
As for the actual code, I was thinking it could be included in the HTML itself, as I'm not very concerned about people viewing the source on their mobile phones.
Is it possible to write a line of code that increments the 'p=' attribute in the URL by one when clicked and only attach it to the correct answers?
Here's an image of what I mean: http://i.imgur.com/HbJ5U.jpg
And, what's to stop me from manually incrementing the "correct answer" counter in my address bar?
Do you not want to use a database because you don't have one available to you in your hosting, or because you don't know how?
I'm not a fan of the idea, but you can get the number of "correct answers" with the following code.
<?php
/* Gets current correct answer Count */
$answer_count = $_GET["p"];
/* checks to see if the submitted answer is the same as the correct answer */
if ($_POST["submitted-answer"] == "correct-answer") {
$answer_count++;
}
?>
Now, you just add the modified answer count to the link to the next question.
Next Question
If this is "just for fun" I don't see why you couldn't do it like this. It's definitely a simple way to solve the problem.
The standard way to do this is to store things in hidden form variables. Of course, if there is anything riding on this, that's a terrible way to do it, because it's really easy for the end user to put his own values in those hidden form values.
Aren't file-based sessions the obvious answer here?

Categories