Summary of some common problems in Php
1: Why can’t I get the variable
I POST data name from one web page to another web page, why can’t I get any value when I output $name?
After PHP 4.2 In this version, register_global defaults to off
If you want to get the variables submitted from another page:
Method 1: Find register_global in PHP.ini and set it to on.
Method 2: Put this extract($_POST);extract($_GET); at the front of the receiving webpage (note that there must be Session_Start() before extract($_SESSION)).
Method 3: Read variables $a= one by one $_GET["a"];$b=$_POST["b"], etc. Although this method is troublesome, it is safer.
2: Debug your program
while it is running You must know the value of a variable.This is what I did, create a file debug.php with the following content:
PHP code:------------------------ -------------------------------------------------- ------
Ob_Start();
Session_Start();
Echo "
";<br><br>Echo "this The _GET variables obtained by this page are: ";<br>Print_R($_GET);<br><br>Echo "The _POST variables obtained by this page are: ";<br>Print_R($_POST);<br> <br>Echo "The _COOKIE variables obtained on this page are:";<br>Print_R($_COOKIE);<br><br>Echo "The _SESSION variables obtained on this page are:";<br>Print_R($ _SESSION);<br>Echo "";
";<br>Echo $_GET["Name"];<br>Echo "";
";
Echo "Here shows all the functions supported by the system, and the custom function phpn";
print_r($arr);
echo " pre>";
?>
---------------------------------------- --------------------------------------------------
10: How to compare the number of days between two dates
PHP code:-------------------------- -------------------------------------------------- ----
$Date_1="2003-7-15";//It can also be: $Date_1="2003-6-25 23:29:14";
$Date_2="1982-10-1";
$Date_List_1=explode("-",$Date_1);
$Date_List_2=explode("-",$Date_2);
$ d1=mktime(0,0,0,$Date_List_1[1],$Date_List_1[2],$Date_List_1[0]);
$d2=mktime(0,0,0,$Date_List_2[1],$ Date_List_2[2],$Date_List_2[0]);
$Days=round(($d1-$d2)/3600/24);
Echo "I have struggled for $Days days^_^";
?>
----------------------------------------- ------------------------------------------
11: Why after I upgraded PHP, the original program showed a full screen Notice: Undefined variable:
This is a warning, caused by the variable being undefined.
Open php.ini , find the error_reporting at the bottom, change it to error_reporting = E_ALL & ~E_NOTICE
For Parse error error
error_reporting(0) cannot be closed.
If you want to close any error prompts, open php.ini , find display_errors and set it to display_errors = Off. Any errors in the future will not be prompted.
Then what is error_reporting?
12: I want to report it at the beginning of each file , add a file at the end. But it is troublesome to add one by one
1: Open the php.ini file
and set include_path= "c:"
2: Write two files
auto_prepend_file.php and auto_append_file.php are saved in the c drive, and they will be automatically attached to the head and tail of each php file.
3: Found in php.ini:
Automatically add files before or after any PHP document. >
PHP code:---------------------------------------------- -------------------------------------
Include "auto_prepend_file.php" ;
.....//Here is your program
Include "auto_append_file.php";
?>
------------------------------------------------ ----------------------------------
13: How to use PHP to upload files
PHP code: ---------------------------- -------------------------------------------------- --Upload file form
You uploaded a file:";
echo $_FILES['upload_file']['name'] ;
echo "
";
//The original name of the client machine file.
Echo "The MIME type of the file is:";
echo $_FILES['upload_file']['type'];
//The MIME type of the file, the browser needs to provide this information Supported, such as "image/gif".
echo "
";
Echo "Upload file size:";
echo $_FILES['upload_file']['size'];
//Uploaded file The size, in bytes.
echo "
";
Echo "The file is temporarily stored as:";
echo $_FILES['upload_file']['tmp_name'];
/ /The temporary file name stored on the server after the file is uploaded.
echo "
";
$Erroe=$_FILES['upload_file']['error'];
switch($Erroe){
case 0 :
case 1:
echo "uploaded files that exceed the value of upload_max_filesize options in php.ini."; Break;
case 2: Echo "The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form."; break;
case 3:
Echo "Only part of the file was uploaded"; break;
case 4:
Echo "No files uploaded";break;
}
?>
-------------------------- -------------------------------------------------- -------
14: How to configure the GD library
The following is my configuration process
1: Use dos command (also You can do it manually, copy all dll files in the dlls folder to the system32 directory) copy c:phpdlls*.dll c:windowssystem32
2: Open php.ini
and set extension_dir = "c:/php/extensions/ ";
3:
extension=php_gd2.dll; Remove the comma in front of extension. If there is no php_gd2.dll, the same is true for php_gd.dll. Make sure that this file does exist c:/php/extensions/php_gd2. dll
4: Run the following program for testing
PHP code:-------------------------------- --------------------------------------------------
Ob_end_flush();
//Note that no information can be output to the browser before this. Pay attention to whether auto_prepend_file is set.
header ("Content- type: image/png");
$im = @imagecreate (200, 100)
or die ("Unable to create image");
$background_color = imagecolorallocate ($im, 0,0, 0 );
$text_color = imagecolorallocate ($im, 230, 140, 150);
imagestring ($im, 3, 30, 50, "A Simple Text String", $text_color);
imagepng ( $im);
?>
---------------------------------- --------------------------------------------------
Click here to view the results
15: What is UBB code
UBB code is a variant of HTML and is the Ultimate Bulletin Board (a foreign BBS program, also used in many places in China) uses a special TAG.
Even if the use of HTML is prohibited, you can still use UBBCode? to achieve it. Maybe you would rather use UBBCode? instead HTML, even if the forum allows the use of HTML, it is safer to use because it requires less code.
Q3boy’s UBB has examples, you can run the test directly
16: I want to modify MySQL user, password
First of all, let me declare that in most cases, modifying MySQL requires root permissions in mysql,
so ordinary users cannot change the password unless requesting the administrator.
Method 1
Use phpmyadmin, this is the simplest, modify the user table of the mysql library,
But don’t forget to use the PASSWORD function.
Method 2
Use mysqladmin, which is a special case stated earlier.
mysqladmin -u root -p password mypasswd
After entering this command, you need to enter the original password of root, and then the root password will be changed to mypasswd.
Change root in the command to your username, and you can change your own password.
Of course, if your mysqladmin cannot connect to the mysql server, or you cannot execute mysqladmin,
then this method is invalid.
And mysqladmin cannot clear the password.
The following methods are used at the mysql prompt and must have root permissions for mysql:
Method 3
mysql> INSERT INTO mysql.user (Host,User,Password)
VALUES('%','jeffrey',PASSWORD('biscuit'));
mysql> FLUSH PRIVILEGES
To be exact, this is adding a user with the username jeffrey and the password biscuit.
There is this example in the "mysql Chinese Reference Manual", so I wrote it out.
Note that you need to use the PASSWORD function, and then use FLUSH PRIVILEGES.
Method 4
Same as method 3, except that the REPLACE statement is used
mysql> REPLACE INTO mysql.user (Host,User,Password)
VALUES('%','jeffrey ',PASSWORD('biscuit'));
mysql> FLUSH PRIVILEGES
Method 5
Use the SET PASSWORD statement,
mysql> SET PASSWORD FOR jeffrey@"%" = PASSWORD(' biscuit');
You must also use the PASSWORD() function,
but there is no need to use FLUSH PRIVILEGES.
Method 6
Use GRANT... IDENTIFIED BY statement
mysql> GRANT USAGE ON *.* TO jeffrey@"%" IDENTIFIED BY 'biscuit';
Here is the PASSWORD() function is unnecessary, and there is no need to use FLUSH PRIVILEGES.
Note: PASSWORD() [does not] perform password encryption in the same way as Unix password encryption.
17: I want to know which website he connected to this page through
PHP code:------------ -------------------------------------------------- ------------------
//You must enter through a super connection to have output
Echo $_SERVER['HTTP_REFERER '];
?>
---------------------------------- ------------------------------------------------
18: What should you pay attention to when putting data into the database and taking it out to display on the page
When entering the database
$str=addslashes($str);
$sql=" insert into `tab` (`content`) values('$str')";
When leaving the library
$str=stripslashes($str);
When displaying
$str=htmlspecialchars( nl2br($str)) ;
19: How to read the current address bar information
PHP code:- -------------------------------------------------- --------------------------
$s="http:// {$_SERVER['HTTP_HOST']}:{$_SERVER["SERVER_PORT"]}{$_SERVER['SCRIPT_NAME']}";
$se='';
foreach ($_GET as $key = > $value) {
$se.=$key."=".$value."&";
}
$se=Preg_Replace("/(.*)&$/", "$1",$se);
$se?$se="?".$se:"";
echo $s."?$se";
?>
- -------------------------------------------------- --------------------------
20: I clicked the back button , why the things you filled in before are missing
This is because you used session.
Solution:
PHP code:------------- -------------------------------------------------- ----------------
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); //Display background
ImageTTFText($img,10,0,25,28,$textColor,"d:/windows/fonts/Tahoma.ttf","your ip is".$ip); // Display 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() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3 = iptype3();
if (isset ($ip1) && $ip1 != "none" && $ip1 != "unknown") {
return $ip1;
}
elseif (isset($ip2) && $ip2 != "none " && $ip2 != "unknown") {
return $ip2;
}
elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown") {
return $ip3;
}
else {
return "none";
}
}
Echo ip();
?>
------------------------------------------------ --------------------------------
23: How to read from the database Get all the records within three days
First of all, there must be a DATETIME field in the table to record the time,
The format is '2003-7-15 16:50:00'
SELECT * FROM ` xltxlm` WHERE TO_DAYS(NOW()) - TO_DAYS(`date`) <= 3;
24: How to remotely connect to Mysql database
when adding users There is a host field in the mysql table, change it to "%", or specify the IP address that allows the connection, so that you can call it remotely.
$link=mysql_connect("192.168.1.80:3306","root","");
25: How to use regular expressions
Click here
Special characters in regular expressions
26: After using Apache, the homepage appears garbled
Method 1:
AddDefaultCharset ISO-8859-1 Change AddDefaultCharset off
Method 2:
AddDefaultCharset GB2312
============================== ==========================
tip:
When you post the code, GB2312 will be interpreted as??????
If you change it to this, it won’t happen