Document directory
- 1. Write a function, array_remove (), which requires that the items with the most concise value of $ V be cleared.
- 2. What are the security issues with the following login code?
- 3. Write a function to calculate the relative path of the two files.
- 2. How can I view the total number of connections on port 1111 of the current server?
- 3. How many processes with Apache names are listed in the current process?
1. Write a function, array_remove (), which requires that the items with the most concise value of $ V be cleared.
Resolution:
The problem is concise and does not need to be explained too much. Assume that the function prototype is array_remove ($ V, $ array ). Note the following:
<1> check the parameters. If $ array is not an array, an error is returned or an exception is thrown.
<2> How do I compare the values of an array with $ V? Is = OR =? There are differences here. For the sake of simplicity, we assume it is a = comparison.
Now we add some restrictions. How can we solve the following restrictions:
A. if you do not use system functions. How can this problem be solved?
B. Question about parameter transfer: How does passing reference and value affect function writing? Implemented separately.
C. How to solve the problem when using system functions.
First, let's look at condition 1: If the system function is not used. How can this problem be solved? Directly traverse it. If a project with a value equal to $ V is encountered, delete it (or record the element with a value not equal to value ). Therefore, there are two forms:
<? PHP/** Method 1: directly pass the reference and modify the original array */function array_remove ($ V, & $ array) {If (! Is_array ($ array) {Throw new exception ("input Param is not a legel, shocould be array \ n");} foreach ($ array as $ key => $ value) {if ($ value = $ v) {unset ($ array [$ key]) ;}}/** method 2. transfer value. Returns the modified array. */Function array_remove_return ($ V, $ array) {If (! Is_array ($ array) {Throw new exception ("input Param is not a legel, shocould be array \ n") ;}$ result = array (); foreach ($ array as $ key => $ value) {if ($ value! = $ V) {$ result [$ key] = $ value ;}return $ result ;}$ test = array (1, 2, 3, 4, 5 ); $ Test2 = array (1, 2, 3, 4, 5); array_remove (3, $ test); print_r ($ test); $ res = array_remove_return (3, $ Test2 ); print_r ($ res );
For Condition B. For the two solutions, see the two solutions of Condition.
Restriction C: If you can use system functions. What are the solutions?
There are also many solutions. Basically, we can use array_search to find the index whose value is equal to $ V and then delete the corresponding element. The Code is as follows:
function array_remove_with_inside($v,&$array){if(!is_array($array)){throw new Exception("input param is not a legel ,should be array\n");}$key = array_search($v,$array);if($key !== false){unset($array[$key]);}}
In the code, note the sixth line: if ($ key! = False ). The comparison here is! = Instead! =. Why? This is because:
Array_search ($ V, $ array); the result may be 0 (the first element of the array ). In PHP, 0 = false, but 0! = False. This is a note. Similar to this case, when traversing a directory. While ($ file = readdir ($ handler ))! = False) This prevents errors in directories or files named 0.
You can also use array_walk to delete the corresponding elements. The code will be supplemented later.
Similar questions include writing reverse ($ string), which is a basic capability for writing database functions. The main test points are efficiency and parameter detection, most of the error handling needs to be carefully and carefully considered.
2. What are the security issues with the following login code?
<? PHP
$ M = $ _ Get ['M'];
$ User = $ _ Get ['user'];
$ Pass = $ _ Get ['pass'];
Include ($ M. "Do. php ");
$ SQL = "select count (*) as B from user where username = '". $ user. "' and Password = '". $ pass ."';
$ R = mysql_query ($ SQL );
List ($ count) = @ mysql_fetch_array ($ R );
If ($ count> 1 ){
...... Login successful
}
?>
Resolution:
Observe the code carefully. We found that there are still many security risks in the code. There are several types.
<1> both the user name and password are transmitted in plain text through get. This is a serious problem. Although the post method is not secure, the POST method is the best for form submission.
<2> User input is not filtered. There are serious SQL Injection risks. Htmlspecialchars, trim, strip_tags should be used to process user input. There is a principle: Never trust user input. This is the truth.
<3> SQL query section. What is the plaintext saved in the database? At least MD5 encryption should be performed, and then the MD5 encryption result should be compared.
<4> include ($ M. "Do. php"); if the user input is used as a parameter without processing, the exception "file does not exist" may occur.
Of course, there is another error that is not a security risk: if ($ count> 1) the question is incorrect. Will there be more than two users with the same user name and password? If ($ count> = 1) (Well suppose there is, at least there should be = 1)
3. Write a function to calculate the relative path of the two files.
For example:
- $ A = '/A/B/C/D/E. php ';
- $ B = '/a/B/12/34/C. php ';
Then, the relative path of $ a relative to $ B is ../C/D/E. php. For any two given directories (the directory format is valid and does not require verification ). Find the relative path of directory 1 to directory 2.
The original function is getrelativedir ($ dir1, $ dir2) to find the relative path of $ dir1 to $ dir2.
Resolution:
The simplest idea. Record the same parts of the two directories and count them. The number of times that the dir2 record exists in the same directory. And add "../". Finally, we can find the relative path of directory 1 to directory 2.
The Code is as follows:
function getRelativePath($dest_dir,$relative_dir){$path_dest = explode('/',dirname($dest_dir));$path_relative = explode('/',dirname($relative_dir));$aLen = count($path_dest);$bLen = count($path_relative);$minLen = min($aLen,$bLen);$count = 0;for($i = 0; $i < $minLen; $i++){if( $path_dest[$i] == $path_relative[$i] ){$count++;}}$path = '';$res_count = $bLen - $count - 1;for($i = 0;$i < $res_count;$i++){$path.= "../";}for($i = $res_count;$i < $aLen;$i++){$path.= $path_dest[$i]."/";}$path.= basename($dest_dir);return $path;}$a = '/a/b/c/f/d/e.php';$b = '/a/b/f/g/k/h/j.php';echo getRelativePath($a,$b);
3. Write a program for remote capturing
Http://www.google.cn/search? Q = php web page content, and match all hyperlink addresses in the web page of this web page, and write them into url.txt.
Resolution: Basic test site: file_get_contents or curl to obtain the network content. Regular Expression match. File_put_contents or file operation functions. Not too difficult.
As a test (Google was attacked by the wall), the search results of Baidu were captured. The test code is as follows:
<?php$url = "http://www.baidu.com/s?wd=php";set_time_limit(0);$content = '';try{$result = getContentFromUrl($url);foreach($result as $key=>$value){if($value != "#"){$content.= $value."\n";}}file_put_contents('url.txt',$content);}catch(Exception $e){echo $e->getMessage();}function getContentFromUrl($url){if(!$url){return null;}$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$content = curl_exec($ch);if($content === false){throw new Exception("[ curl ]:".curl_erron().":".curl_error());}curl_close($ch);//$content = file_get_contents($url);$result = array();preg_match_all('/<a(?: +[^>]*)? *href *= *"([^"]*)" *[^>]*?>(?:.*)<\/a>/i',$content,$result);var_dump( $result[1] );return $result[1];}
MySQL section:
1. How can I check the efficiency index usage of SQL statements?
Resolution: Explain SQL statement. The corresponding results can be seen from the table.
For more details, refer to this article: http://www.1983blue.com/index.php/posts/mysql-explain-index_1
JS section:
<Form action = "? Do = login "name =" login "method =" post "> <input type =" text "value =" "name =" age "/> </form>
How can I change the form action to 18 when the age value is set to 18? Do = login_bak?
Resolution: simple event listening and JS functions.
Onchange checks the value. If it is 18. Then form. login. Action =? Do = login_bak.
Server:
1. What is the current server load command?
Resolution: uptime
, Top command. Loadavg. Proc system.
Recommended articles: http://www.flybaaa.com/help/69_1.html. The explanation is clear and clear.
2. How can I view the total number of connections on port 1111 of the current server?
Netstat-Nat | grep: 1111 | WC-l
3. How many processes with Apache names are listed in the current process?
PS-Ef | grep Apache
Others
What you need to do now is an image management system. Upload user images. How do you avoid image theft. 2. The image is uploaded repeatedly. How do I set the upload and save directory?
Resolution: It should be noted that "anti-theft" is different from "anti-leeching.
To prevent theft, you need to do something like a watermark (for example, the image uploaded on Sina Blog will automatically add your Weibo ID, which is a anti-theft technology ).
How to Prevent image theft, can refer to this article: http://blog.sina.com.cn/s/blog_5e3e2a6a0100e658.html
To prevent leeching, you need to set the directory or file permissions, or perform domain name detection (when the Domain Name of the website is not the domain name, the prompt is displayed, or an alternative image is displayed ). Apache directory settings can implement this function.
For details, see http://blog.csdn.net/mygood322/article/details/7582531.
The upload directory is generally set to a large directory for a user. Directory can be divided into multiple specific directories.
To prevent repeated image uploads, you can:
Prohibit Multiple submission buttons in a short period of time (JS control, the upload button becomes invalid several seconds after the button is clicked );
To prevent the refresh and submit button; for this, see a very concise method:
Use the token mechanism to generate a random string that exists in the session. For example: If (! Isset ($ _ session ['verify ']) {$ _ session ['verify'] = gmmktime ();} add a hidden field <input type = "hidden" name = "verify" value = "<? = $ _ Session ['verify ']?> "/> When submitting a form, check whether the submitted verify value is equal to the IF ($ _ post ['verify '] stored in the session. = $ _ Session ['verify ']) {die ('Do not submit repeatedly');} else {unset ($ _ session ['verify ']); // do what you need to do for upload .....}
The PHP interview questions are basically summarized.