driz Posted September 14, 2009 Share Posted September 14, 2009 I'm trying to create a page that will use PHP to detect a keydown so for example if a user is on my page and types ALT + A it will do a function.Is this possible? I don't want to use JS as JS can be viewed in the source, and I don't it to be viewed! Thanks Link to comment Share on other sites More sharing options...
jeffman Posted September 14, 2009 Share Posted September 14, 2009 It is certainly not possible. PHP runs on your server, not in the browser. The only way PHP could respond to a browser event is if the browser communicated the event to PHP. It could do this through a link, a form, or AJAX. Either of these methods would be quite visible to your user. And since you're talking about key events, the waiting time would be ANNOYING.A lot of user interaction HAS to take place on the browser. In fact, the trend these days is to INCREASE the amount of JavaScript programming in browser documents. You just have to live with the idea that techniques and code will be borrowed.Considering the fact that PHP comes to you FREE, and your browsers come to you FREE, it's really not a bad deal. Link to comment Share on other sites More sharing options...
driz Posted September 14, 2009 Author Share Posted September 14, 2009 The reason I want the code to be hidden is for a security sort of standpoint. The idea is create a back-end login system that is accessed via a set a keystrokes. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 14, 2009 Share Posted September 14, 2009 Then use the keystroke to send an ajax request to run the PHP code. PHP can't detect browser events, and Javascript can't run on the server. You need both. Link to comment Share on other sites More sharing options...
driz Posted September 14, 2009 Author Share Posted September 14, 2009 Then use the keystroke to send an ajax request to run the PHP code. PHP can't detect browser events, and Javascript can't run on the server. You need both.How do I do that? I have read about something called keycodes (are they needed or just another way) but I don't understand how to grab em and then send to php.To break it down: User types SHIFT + ~ and then the php runs a function like:function keyRedirect() { header("Location:http://auth.mydomain.com/login.php"); } Edit: The key combination can't be seen at all in the JS, it would have to be done all inside PHP otherwise someone could see what the combination was to access the pageSo JS basically just grabs the keystrokes and fires them off to PHP and that's all. The PHP checks the key strokes and if they match those mentioned then they run the function. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 14, 2009 Share Posted September 14, 2009 Which part don't you understand? Have you used ajax before? Link to comment Share on other sites More sharing options...
driz Posted September 14, 2009 Author Share Posted September 14, 2009 Which part don't you understand? Have you used ajax before?Yes but I don't understand how to use it with keystrokes and use it with a PHP function that is looking for that particular key stroke. Link to comment Share on other sites More sharing options...
Ingolme Posted September 14, 2009 Share Posted September 14, 2009 Yes but I don't understand how to use it with keystrokes and use it with a PHP function that is looking for that particular key stroke. Javascript detects the key. Based on the key that was pressed, it send a specified request to the server with AJAX. The server runs a script and gives a response. Javascript does something with the response. Link to comment Share on other sites More sharing options...
jeffman Posted September 14, 2009 Share Posted September 14, 2009 PHP isn't looking for a keystroke. Here's your basic flowchart. User hits a key. A handler in javaScript is triggered by the keystroke. The handler sends an AJAX message to the server. The message contains information related to the keystroke. The PHP script that got contacted responds in a way that matches the key stroke. Javascript does something with the response that comes from the server. Link to comment Share on other sites More sharing options...
jeffman Posted September 14, 2009 Share Posted September 14, 2009 When two programmers independently come back with basically the same procedure, trust it. Link to comment Share on other sites More sharing options...
driz Posted September 14, 2009 Author Share Posted September 14, 2009 Thanks for all the info, but I don't know how to do that. Link to comment Share on other sites More sharing options...
Ingolme Posted September 14, 2009 Share Posted September 14, 2009 Thanks for all the info, but I don't know how to do that.I was under the impression that you had used AJAX before, so I thought you'd understand that.You should try looking through the W3Schools AJAX tutorial to learn how to send requests to the server and use the response that the server sends back. Link to comment Share on other sites More sharing options...
jeffman Posted September 14, 2009 Share Posted September 14, 2009 You need a keydown handler, probably attached to the window so you catch all keystrokes. You've probably written one of these:window.onkeydown = //somethingMostly you need to learn AJAX. Yes, it's a steep learning curve.If you're already scripting in PHP or ASP or something, then there won't be much difference. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.