I’m trying to execute Javascript on clients browsers based on what their IP address is. I’ve got a table of user info, like their name, and another table that has IP’s and what Javascript gets executed on them.
For example, I want everyone with the IP 192.168.0.* (my local network) to be redirected to Google every time they view this page. In the database, the IP listed is 192.168.0.%, and in the javascript column I have window.location.href = 'http://www.google.com';
Someone with the IP 192.168.0.5 goes to my page. The PHP runs an SQL query to find their IP and check if there’s any specific Javascript that needs to be executed (if it’s not in the database, nothing happens to the client). It SHOULD find that window.location.href = 'http://www.google.com'; needs to be executed, because they fit the wildcard.
Can I keep wildcards in entries in the SQL table and still have them behave as if they were being used in a query? Would mysql_query("SELECT javascript_to_execute FROM ip_table WHERE ip='$user_IP'"); work?
Yes, you can store the wildcard character % as part of a string. The IP range 192.168.0.* could be saved in your database as
192.168.0.%. The query would then beSELECT javascript FROM ip_table WHERE '$user_IP' LIKE ip