简体   繁体   English

在 MySQL 中存储 PHP 片段

[英]Storing PHP snippets in MySQL

I am storing PHP snippets in a MySQL database, I am using mysql_real_escape_string and all is well unless there is a & in the php code and then I get a MySQL error. I am storing PHP snippets in a MySQL database, I am using mysql_real_escape_string and all is well unless there is a & in the php code and then I get a MySQL error. Is there another why I should try and store this information?还有其他为什么我应该尝试存储这些信息吗?

Thanks谢谢

@Peter: unless you're building a website for helping developers, you have no reason to put php code into your database, it's a warning: this is gonna be a big nightmare to maintain/debug. @Peter:除非您正在建立一个帮助开发人员的网站,否则您没有理由将 php 代码放入您的数据库中,这是一个警告:这将是维护/调试的一场噩梦。 Can't you link your pages to some parameters and then in your code use these parameters to build each request?您不能将您的页面链接到一些参数,然后在您的代码中使用这些参数来构建每个请求吗? it may seems a simple design solution at the beginning "how god I can do whatever I want in all my pages" but it might be the worse you're taking on your poject.一开始它似乎是一个简单的设计解决方案“上帝啊,我怎么能在我的所有页面中做任何我想做的事情”,但它可能会让你的项目变得更糟。

I don't know how to say this but you should really try to consider an other solution.我不知道该怎么说,但你真的应该尝试考虑其他解决方案。 And i'm not speaing about security: if you have an SQL Injection the guy can execute SQL AND php so he can really take all your system/server down, or even attack bigger site with yours (and then you'll be responsible).而且我并不是要担心安全:如果您有Z977840A0100CB30C982876741B0B0B5A2Z注入,该家伙可以执行Z97778840A0100CB30C98282876741B0B5A2Z和ZE1BFD762323221E4092322221EERY4982828741B0 BESTERING yourter yound yours yours yours yours yours yours yours yours yours yours yours yours yours yon4409922221e4409922221e4409922221e4409 .

I'm really surprised everyone is fine with it.我真的很惊讶每个人都很好。

Use base64_encode when you save snippet into the database and base64_decode when you retreive it.将代码片段保存到数据库时使用base64_encode ,检索它时使用base64_decode

First, I am going to go on record and say I wholeheartedly agree with remi bourgarel.首先,我要记录在案的 go 并说我完全同意 remi bourgarel。 This is likely a bad idea.这可能是个坏主意。

But, from a technical standpoint here's how I'd do this IF I NEEDED TO:但是,从技术角度来看,如果需要,我会这样做:

$php_code = '
    <?php
        $var = "this is a string";
        $var = strtoupper($var);
        echo $var;
    ?>
';
$php_code = bin2hex($php_code);
$db->query("INSERT INTO php_code_snips (text_code) VALUES(x'{$php_code}')");

bin2hex will transform the string $php_code from a binary string to a hex string, and the x'{$php_code}' tells mysql to expect a hex string. bin2hex会将字符串$php_code从二进制字符串转换为十六进制字符串,并且x'{$php_code}'告诉 mysql 期待十六进制字符串。

This means the string is stored as a string in the DB, and is fully searchable.这意味着字符串作为字符串存储在数据库中,并且是完全可搜索的。 But, since all chars are encoded as hex during the INSERT the special chars won't cause a problem.但是,由于在INSERT期间所有字符都被编码为十六进制,因此特殊字符不会引起问题。

Documentation:文档:

bin2hex bin2hex

Mysql Hex Values Mysql 十六进制值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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