Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/242768-htaccess-causing-index-to-load-twice/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.