PHP Master lead the way: Problem summary Answer (2)

Source: Internet
Author: User
Tags definition eval flock flush header insert php code mysql database

  [Review]: The first section describes "debugger", "How to use Session", "specification SQL statements" and so on 15 questions (PHP Master lead the way-problem summary solution [1]). This episode continues to provide answers to 16 common questions.

16: I want to modify the MySQL user, password
The first thing to declare is that in most cases, MySQL is required to have root privileges in MySQL,

Therefore, the general user cannot change the password unless the administrator is requested.

  Method One

Using phpMyAdmin, this is the easiest, to modify the MySQL library's user table,

But don't forget to use the password function.

  Method Two

Using Mysqladmin, this is a special case of the previous declaration.

Mysqladmin-u root-p Password mypasswd

After entering this command, you need to enter the original password for root, and then the root password will be changed to MYPASSWD.

Change the command root to your username, and you can change your password.

Of course, if your mysqladmin is not connected to MySQL server, or you have no way to execute mysqladmin,

Then this method is ineffective.

And mysqladmin can't empty the password.
The following methods are used at the MySQL prompt and must have the root permissions of MySQL:

  Method Three

Mysql> INSERT into Mysql.user (Host,user,password)

VALUES ('% ', ' Jeffrey ', PASSWORD (' biscuit '));

Mysql> FLUSH Privileges

To be exact, this is adding a user, the username is Jeffrey, and the password is biscuit.

There is this example in the MySQL Chinese reference manual, so I wrote it.

Note that you want to use the password function, and then use flush privileges.

  method Four

And method Three, just use the Replace statement

Mysql> REPLACE into Mysql.user (Host,user,password)

VALUES ('% ', ' Jeffrey ', PASSWORD (' biscuit '));

Mysql> FLUSH Privileges

  Method Five

Using the Set Password statement,

Mysql> SET PASSWORD for jeffrey@ "%" = PASSWORD (' biscuit ');

You must also use the password () function,

But you don't need to use flush privileges.

  Method Six

Use Grant ... Identified by statement

Mysql> GRANT USAGE on *.* to jeffrey@ "%" identified by ' biscuit ';


Here the password () function is unnecessary and does not require the use of flush privileges.
Note: PASSWORD () [is not] the password encryption is performed in the same way as the UNIX password encryption.


17: I want to know which website he is connecting to this page
PHP Code:

<?php

Must go through the super connection to have output

Echo $_server[' Http_referer '];

?>

18: Data into the database and take out to display in the page need to pay attention to what
When in storage

$str =addslashes ($STR);

$sql = "INSERT INTO ' tab ' (' Content ') VALUES (' $str ')";

When you are out of the library

$str =stripslashes ($STR);

When displayed

$str =htmlspecialchars (NL2BR ($STR));
<?php

$content from the database

$content =nl2br (Htmlspecialchars ($content));

$content =str_replace ("", "", $content);

$content =str_replace ("\ n", "<br>\n", $content);

?>

19: How to read the current address bar information
PHP Code:

<?php

$s = "http://{$_server[' http_host ']}:{$_server[" server_port "]}{$_server[' Script_name ']}";

$se = ';
foreach ($_get as $key => $value) {
$se. = $key. " = ". $value." & ";
}
$se =preg_replace ("/(. *) &$/", "$", $se);
$se? $se = "?". $se: "";
echo $s. "? $se ";
?>

20: I click on the Back button, why the missing items before filling
This is because you use the session.

Solution:
PHP Code:

<?php Session_cache_limiter (' Private, must-revalidate '); Session_Start ();
.....................? >

21: How to display the IP address in the picture

PHP Code:

? Header ("Content-type:image/png");

$img = Imagecreate (180,50);
$ip = $_server[' remote_addr '];

Imagecolortransparent ($img, $bgcolor);

$bgColor = Imagecolorallocate ($img, 0X2C,0X6D,0XAF); Background color

$shadow = Imagecolorallocate ($img, 250,0,0); Shadow color

$textColor = Imagecolorallocate ($img, Oxff,oxff,oxff); Font Color

Imagettftext ($img, 10,0,78,30, $shadow, "D:/windows/fonts/tahoma.ttf", $IP);
Show background

Imagettftext ($img, 10,0,25,28, $textColor, "D:/windows/fonts/tahoma.ttf", "Your IP is". $ip);

Show ip

Imagepng ($IMG);

Imagecreatefrompng ($IMG);
Imagedestroy ($IMG);

?>

22: How to get the user's real IP

PHP Code:

? function Iptype1 () {

if (getenv ("Http_client_ip"))

{
Return getenv ("Http_client_ip");

}

Else

{

return "None";
}

}

function Iptype2 () {

if (getenv ("Http_x_forwarded_for"))

{

Return
Getenv ("Http_x_forwarded_for");

}

else {

return "None";
}

}

function Iptype3 () {

if (getenv ("REMOTE_ADDR"))

{

Return getenv ("REMOTE_ADDR");
}

else {

return "None";

}

}

function IP () {

$ip 1 = iptype1 ();

$ip 2 = iptype2 ();

$ip 3 = Iptype3 ();

if (Isset ($ip 1) && $ip 1!= "None" && $ip 1!= "Unknown")

{

return $IP 1;

}

ElseIf (Isset ($ip 2) && $ip 2!= "None" && $ip 2!= "Unknown")
{

return $IP 2;

}

ElseIf (Isset ($ip 3) && $ip 3!= "None" && $ip 3!= "Unknown")

{

return $IP 3;

}

Else

{return ' none ';}

}

Echo IP ();

?>


23: How to read all records in three days from the database
First, there is a datetime field in the table to record the time,

Format is ' 2003-7-15 16:50:00 '
SELECT * from ' xltxlm ' WHERE to_days (now ())-to_days (' Date ') <= 3;


24: How to remotely link the MySQL database

In the Add user's MySQL table there is a host field, modified to "%", or specifies the IP address that allows the connection, so that you can call it remotely.
$link =mysql_connect ("192.168.1.80:3306", "Root", "");


25: Exactly how to use
Click here
Special characters in regular expressions

26: With Apache, the homepage appears garbled

Method One:

Adddefaultcharset Iso-8859-1 changed to adddefaultcharset off

Method Two:

Adddefaultcharset GB2312


27: Why single quotes, double quotes in the Accept page become (\ ')
Workaround:

Method One: Set in php.ini: MAGIC_QUOTES_GPC = Off

Method Two: $str =stripcslashes ($STR)


28: How to keep the program running, not more than 30 seconds to stop
Set_time_limit (60)//maximum running time of one minute

Set_time_limit (0)//run to the end of the program itself, or manually stop


29: Calculate the current number of online
Example one: Using text to achieve
PHP Code:

<?php

First, you have permission to read and write files.

This program can be run directly, the first error, you can

$online _log = "Count.dat"; Save the number of documents,

$timeout = 30;//30 in seconds without moving the author, who thinks that the drop line

$entries = File ($online _log);
$temp = Array ();

for ($i =0; $i <count ($entries); $i + +) {

$entry = Explode (",", Trim ($entries [$i]));

if ($entry [0]!= getenv (' remote_addr ')) && ($entry [1] > Time ())
{

Array_push ($temp, $entry [0]. ",". $entry [1]. " \ n "); Remove the other viewer's information and remove the timeout and save it in $temp

}

}
Array_push ($temp, getenv (' remote_addr '). ",". ( Time () + ($timeout)). " \ n ");
Update the viewer's time

$users _online = count ($temp); Calculate online numbers
$entries = Implode ("", $temp);

Write to File

$fp = fopen ($online _log, "w");

Flock ($FP, LOCK_EX); Flock () does not work in NFS and some other network file systems

Fputs ($fp, $entries);

Flock ($FP, lock_un);

Fclose ($FP);
echo "currently has". $users _online. " People online ";
?>


30: What is a template, how to use
I'm using a phplib template.

Here are the uses of several of these functions
$T->set_file ("Casual definition", "template file. TPL");
$T->set_block ("defined in Set_file", "<!--from template-->", "casual definition");

$T->parse ("defined in Set_block", "<!--from template-->", true);

$T->parse ("Random output result", "defined in Set_file");
Set the looping format as:
<!--(more than one space) BEGIN $handle (more than one space)-->
How to generate a static Web page from a template


PHP Code:

<?php

Use the Phplib template here

............

............

$TPL->parse ("Output", "html");

$output = $tpl->get ("output");//$output for entire Web page content


function Wfile ($file, $content, $mode = ' W ') {

$oldmask = umask (0);

$fp = fopen ($file, $mode);

if (! $fp) return false;

Fwrite ($fp, $content);

Fclose ($FP);

Umask ($oldmask);

return true;

}

It's written in a file.

Wfile ($FILE, $output);

Header ("Location: $FILE");//redirect to generated Web page

}

?>
   phplib Download Address smarty Download Address

31: How to interpret the characters in PHP   

For example: Input 2+2* (1+2), auto output 8 can use the Eval function

PHP Code:

<form method=post action= "" >

<input type= "text" name= "str" ><input type= "Submit" >

</form>

<?php

$str =$_post[' str '];

Eval ("\ $o = $str;");

Echo "$o";

?>


To this end, the question of PHP for you to explain the completion of the hope that you can help.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.