SoFunction
Updated on 2025-04-12

Summary of some FAQs in Php


20: I clicked the back button, why did I not see the stuff I filled in before?
This is because you use session.
Solution:
<?php 
session_cache_limiter('private, must-revalidate');
session_start(); 
...........
..........
?>
21: How to display IP address in the picture
<?
  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/",$ip); //Show background
ImageTTFText($img,10,0,25,28,$textColor,"d:/windows/fonts/","your ip is".$ip); // Show IP
  ImagePng($img);
  imagecreatefrompng($img);
  ImageDestroy($img);
?>
22: How to obtain the user's real IP
<? 
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 all records within three days from the database
First, 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
There is a host field in the mysql table of the added user, modify it to "%", or specify the IP address that allows connections, so that you can call it remotely.
$link=mysql_connect("192.168.1.80:3306","root","");
25: How to use regularity
Click here
Special characters in regular expressions
26: After using Apache, garbled code appears on the home page
Method 1:
AddDefaultCharset ISO-8859-1 Change to AddDefaultCharset off
Method 2:
AddDefaultCharset GB2312
========================================================
tip:
When everyone posts the code, GB2312 will be interpreted as??????
Change it to this way, it won't
[color=#000000]GB[/color]2312
10: How to compare how many days apart between two dates (simpler algorithm)
<?PHP
$Date_1="2003-7-15";//It can also be: $Date_1="2003-7-15 23:29:14";
$Date_2="1982-10-1";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d1-$d2)/3600/24);
Echo   "I have already struggled $Days day^_^";
?>
27: Why do single quotes and double quotes become (\\'\") on the accept page
Solution:
Method 1: Set in: magic_quotes_gpc = Off
Method 2: $str=strpcslashes($str)
28: How to keep the program running, instead of stopping it after more than 30 seconds
set_time_limit(60)//Maximum running time per minute
set_time_limit(0)//Run until the program ends itself, or stops manually
29: Calculate the current number of online users
Example 1: Implementation with text
<?php 
//First of all, you need permission to read and write files
//This program can be run directly, and the first error is reported, and then it will be done later.
$online_log = """; // Files that save numbers of people,
$timeout = 30;// Those who do not act within 30 seconds are considered disconnected
  $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"); // Take out the information of other visitors, remove the timeout, and save it in $temp
   } 
  } 
array_push($temp,getenv('REMOTE_ADDR').",",".(time() + ($timeout))."n"); //Update the time of the viewer
$users_online = count($temp); //Calculate the number of online users
  $entries = implode("",$temp); 
//Write to file
  $fp = fopen($online_log,"w"); 
flock($fp,LOCK_EX); //flock() cannot work normally in NFS and some other network file systems
   fputs($fp,$entries); 
   flock($fp,LOCK_UN); 
   fclose($fp); 
echo "Currently there".$users_online."People online";
?>
Example 2:
Use database to realize online users
30: What is a template and how to use it
Here are a few articles about templates
I'm using phplib template
Here are some of the functions used
$T->Set_File("Define at will","Template file.tpl");
$T->Set_Block("defined in set_file","<!-- From template -->","Definitely");
$T->arse("Defined in Set_Block","<!-- From template -->",true);
$T->arse("Order output result","defined in Set_File");
Set the loop format to:
<!--(more than one space) BEGIN $handle(more than one space)-->
How to generate a static web page for templates
<?php
// Use phplib template here
    ............
    ............
    $tpl->parse("output","html");
$output = $tpl->get("output");// $output is the 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;
}
// Write it in the file
    Wfile($FILE,$output);
header("location:$FILE");//Redirect to the generated web page
}
?>
phplib download address                                                            �
31: How to use php to explain characters
For example: input 2+2*(1+2), automatically output 8
You can use the eval function
<form method=post action="">
<input type="text" name="str"><input type="submit">
</form>
<?php
$str=$_POST['str'];
eval("$o=$str;");
Echo   "$o";
?>
In addition, you must be very careful when using this function!!
What would it be if someone entered format: d:?