travishpa Posted July 25, 2011 Share Posted July 25, 2011 Hello, So here's the problem. I have an .htaccess file that rewrites everything except actual filenames and directories to index.php. Here's the current state of .htaccess (after much tinkering) RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] The index.php: <?php include('./classes/class.db.php'); $r = rand(0,100); $db = new DB(DB_ADDRESS,DB_USER,DB_PASS,DB_TABL); $db->test($r); echo $r; die(); class.db.php: <?php class DB { private $pdo; public function __construct($db,$usr,$pass,$tabl){ $this->pdo = new PDO("mysql:host=$db;dbname=$tabl", $usr, $pass); } public function test($r){ $stmt = $this->pdo->prepare("INSERT INTO blah (data,datab) VALUES (:data,:datab)"); $data = array( 'data' => time(), 'datab' => $r); $stmt->execute($data); } public function __destruct(){ $this->pdo = null; } } What the code is meant to do (as of now for testing) is create a prepared statement containing the time() and a random number and echo the number to the output buffer. What it ends up doing is creating two database entries with two time()s (milliseconds apart) and two random numbers. Now the really strange part is this: On the screen it does not show the last random number that it added to the database, but the first! There are no empty image tags, no html calls what-so-ever. It's not a browser extension (tried it on multiple computers and browsers) I'm thinking it has to do with the .htaccess file? But how i'm not sure. It's such a simple redirect, i'm not even using it to parse the urls - simply letting php explode the URI. So tell me, am I doing something stupid? ,Travis Quote Link to comment https://forums.phpfreaks.com/topic/242768-htaccess-causing-index-to-load-twice/ Share on other sites More sharing options...
wildteen88 Posted July 25, 2011 Share Posted July 25, 2011 You're right its calling index.php twice. I have just tested this myself using file_put_contents('data.txt', time() . "\n", FILE_APPEND); In an index.php file. It records two timestamps when calling index.php When web browsers request for files they also request for a file called favicon.ico at the same time. This is what is happening when you call index.php. The browser will first request for the webpage (index.php) and then immediately after it is calling for favicon.ico which what is adding the secondary timestamp to your database. The reason why the favicon.ico file is calling index.php is because your rewriterule is writing everything to the index.php file. Change your rewriteCond block to RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !favicon.ico #ignore the request for favicon.ico RewriteCond %{REQUEST_FILENAME} !-d When you start using images/css/javascript files within your pages you'll also need to ignore the requests for these too. You can change RewriteCond %{REQUEST_FILENAME} !favicon.ico to RewriteCond %{REQUEST_FILENAME} !.(ico,css,js,jpg,gif,png)$ # ignore requests to icons, css, javascript, and images Quote Link to comment https://forums.phpfreaks.com/topic/242768-htaccess-causing-index-to-load-twice/#findComment-1246916 Share on other sites More sharing options...
travishpa Posted July 25, 2011 Author Share Posted July 25, 2011 Brilliant! I didn't realize that browsers would greedily check your server for favicons, but after some research you are correct! Good thinking. Unfortunately the rewrite conditions you suggested didn't change anything. Adding a blank favicon.ico file in fact did cause the script from running twice! Though that's not really a great fix because as you said, any time a file isn't found it's going to run the script. This is my first time just throwing everything blindly at the index script, so it shows that I need to work on my htaccess file some more. Thank you so much for your help, it was an infuriating and unexplainible issue, seems so simple now. Quote Link to comment https://forums.phpfreaks.com/topic/242768-htaccess-causing-index-to-load-twice/#findComment-1246932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.