Jump to content

A Question of Place and Timing


iwato

Recommended Posts

BACKGROUND:  I have three search forms included in three different <div>s on the Grammar Captive main page.  Initially all three of the forms are hidden.  When a visitor selects one of the three forms, the javascript file associated with the main page performs a test in order to ascertain whether previous search data associated with the form is recoverable.  if so, two of two additional javascript files is loaded.  If not, only one of them.  This latter  file is crucial to both scenarios.

GOAL:  As I strongly suspect that my database is being spammed with repeated entries of the same search data, I am in the process of creating an imageless captcha in order to frustrate the spammer, but not the visitor.

DILEMMA:  The code that creates the Captcha is a simply written PHP class called ImagelessCaptcha, and I need to create a new instance of the class each time a new search is made.

CONSIDERED SCENARIO:  Load the PHP class when the mainpage opens and create a new instance from the loaded class each time the one of the pair of aforementioned javascript files is called.

QUESTION: My concern is where and how I should call the new instance.  Is this a problem for AJAX?  Any suggestions?

Roddy

 

 

 

 

Link to comment
Share on other sites

Not exactly!  I have modified a class that someone else designed for a very different environment.

Right now I am concerned about where to include the class, so that its inclusion does not become redundant on the one hand, and so that it is always and everywhere available on the other. There is a three step process involved.

1) Load the HTML mainpage.

2) Call a Javascript document that loads any of three search <div>s on the mainpage into the mainpage's main <div>.

3) Call a another Javascript document that captures the search data and sends it off to a PHP file via AJAX for processing on the mainpage.

Roddy

 

Link to comment
Share on other sites

If you use include_once or require_once to include files in PHP then it will only include the same file once even if there are multiple include statements.  Hopefully you have a main PHP include file that your other PHP files include that defines various global functions and variables, and that would be the place to include any file that you want available on any other page.

  • Like 1
Link to comment
Share on other sites

I do not have what you are suggesting, but you have answered my question with regard to multiple include statements.

I do have one more question before you disappear for the week end.

Will PHP used to create new instances of the same previously called PHP class and to assign values to Javascript variables be properly read when the Javascript file is loaded.  Or, must the assignment be achieved via AJAX or some other, hitherto by me yet to be employed, technique?

Roddy

Link to comment
Share on other sites

Requesting a Javascript file isn't going to run any PHP code unless the web server is configured to send .js files to PHP.  You could name the file with a .php extension if you need to dynamically create Javascript code for whatever reason.

  • Like 1
Link to comment
Share on other sites

A Ha!  A "hitherto by me yet to be employed" technique is now on the table.
Please do not run away for the weekend just yet.

QUESTION ONE: Are you suggesting that the .js file extension is just a file accounting convenience and has no effect on whether Javascript is run?

QUESTION TWO:  Are there any additional security problems associated with setting the server to run javascript (.js) files with PHP?

Roddy

Link to comment
Share on other sites

QUESTION THREE:  By way of confirmation, may I use the same require_once() function as many times and places as I like during the same browser session with good confidence that it will be included/required only once?

Roddy

Link to comment
Share on other sites

Are you suggesting that the .js file extension is just a file accounting convenience and has no effect on whether Javascript is run?

The browser doesn't care what the filename is, just make sure it's right:

<script type="application/javascript" src="image.jpg"></script>

The browser isn't going to complain about an extension, have you ever seen an error message like that?  It's going to download the file you tell it to and treat it like you're telling it to treat it,  the browser assumes that the programmer knows what they're doing.  If the file has an extension that PHP is configured to handle, then when your browser sends a request for the file the web server will send it to PHP and send the output to the browser, like with any other request.

Are there any additional security problems associated with setting the server to run javascript (.js) files with PHP?

It's not a security problem, just efficiency.  There's no reason to have PHP try to parse a bunch of file types that will almost never have PHP code in them.  I just use .js.php when I want to indicate a PHP file that should output Javascript.

By way of confirmation, may I use the same require_once() function as many times and places as I like during the same browser session with good confidence that it will be included/required only once?

That's what it's for.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...