简体   繁体   中英

PHP doesn't save variable as planned

I want to save which page an user is looking at in the database. I wrote the code but everytime it save the page to the database it saves the string "favicon.ico". But when i do a var_dump of the variable i want to save in the database it says "personal/messages".

Here is the code:

$page = isset($_GET['REQUEST_URI']) ? $_GET['REQUEST_URI'] : 'home';

var_dump($page); // Outputs: string(17) "personal/messages"

$this->query(
    "UPDATE ".TBL_USERS." SET page = :page WHERE username = :id",
    array(':page' => $page, ':id' => $username)
);

So when i look in the database it says: "favicon.ico". When i change the variable $page to = "home"; it saves home to the database instead of favicon.ico.

Here are some more outputs:

$page = isset($_GET['REQUEST_URI']) ? $_GET['REQUEST_URI'] : 'home';
var_dump($page); // Outputs: string(17) "personal/messages"
// Saves in database as "favicon.ico"

$page = $_GET['REQUEST_URI'];
var_dump($page); // Outputs: string(17) "personal/messages"
// Saves in database as "favicon.ico"

$page = "personal/messages";
var_dump($page); // Outputs: string(17) "personal/messages"
// Saves in database as "personal/messages"

Does somebody know the problem?

The user's browser does a request to favicon.ico so it can display a lovely favicon. But your htaccess appears to rewrite that request to your script. You either have to place a favicon.ico. Or change your htaccess file

Rather than saving REQUEST_URi

Get the file name from the magic constant __FILE__ and remove extension from it.

function chopExtension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
$page = chopExtension(__FILE__);

It seems that you have an URL rewriting problem (either in your htaccess file or in your server configuration). The problem is that requests for static files are also redirected to your php script, this is why your script is also getting the /favicon.ico request and writing it to the database.

You will have to verify your URL rewriting rules in order to prevent static files URIs from being redirected to your script URI. Guide for Apache Guide for Nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM