PHP网站安装程序制作 (附:模块检测的方法)

Many web applications such as WordPress lead you through the whole installation process from asking for SQL details to getting some user login details etc.

 

Due to the complexity of what these are, I'll be leading you through the installation of a basic fictitious site that includes the following steps:

  1. Enter MySQL details
  2. Set up MySQL Tables etc.
  3. Insert Site Title, Tagline, Contact Email.
  4. Insert Admin User's details [username, display name and password].
  5. Success/Error Page

The SQL

Firstly we need to actually create the SQL that we want to run when the site is being installed - for our needs this is going to simply create two tables: users and settings.

users will have 5 columns:

  • user_id (Auto-Increment, Primary Key, Int)
  • username (Varchar, 30)
  • display_name (Varchar, 50)
  • password (Varchar, 40)
  • admin (Int, 1)

settings will have 3 columns:

  • setting_id (Auto-Increment, Primary Key, Int)
  • setting_name (Varchar, 30)
  • setting_value (Varchar, 100)

I understand that having the settings table constructed like this is probably not the best setup as not all settings will fit into the varchar setup, this is something for you to consider when you're working away. With those basics worked out, we can write some SQL…

Sql代码  收藏代码
  1. CREATE TABLE `users` (  
  2.     `user_id` int(11) auto_increment,  
  3.     `username` varchar(30),   
  4.     `display_name` varchar(50),  
  5.     `passwordvarchar (40),  
  6.     `admin` int(1),  
  7.     PRIMARY KEY (`user_id`)  
  8. );  
  9. CREATE TABLE `settings` (  
  10.     `setting_id` int(11) auto_increment,  
  11.     `setting_name` varchar(30),   
  12.     `setting_value` varchar(100)  
  13.     PRIMARY KEY (`setting_id`)  
  14. );  

 

Let's Write Some HTML & CSS

As there's not a huge amount of data that is required to be entered, we are going to throw all of this on a single form for the user to complete, you can break this page down into steps if you want using some JavaScript or something else. However, that's beyond the scope of this post for now.

 

So what I've done just quickly is thrown together a form, there's nothing fancy about it at all, it just has a few input boxes for the user to enter their details etc. At the moment, it's a basic HTML form - we will in the next step create the PHP page that it posts to.

 

This HTML is simpler than the example, due to character limits on Forrst, I had to strip the CSS/Labels etc out.

Html代码  收藏代码
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <title>PHP Installer - @MichaelW90</title>  
  6. </head>  
  7. <body>  
  8.     <form method='post' action='./install.php'>  
  9.         <p>  
  10.             <h2>MySQL Details:</h2>  
  11.             MySQL Host: <input type='text' name='sql-host' /><br />  
  12.             MySQL Username: <input type='text' name='sql-username' /><br />  
  13.             MySQL Password: <input type='text' name='sql-password' /><br />  
  14.             MySQL Database: <input type='text' name='sql-database' /><br />  
  15.         </p>  
  16.         <p>  
  17.             <h2>Site Details:</h2>  
  18.             Site Name: <input type='text' name='settings-sitename' /><br />  
  19.             Site Tagline: <input type='text' name='settings-sitetagline' /><br />  
  20.             Site Contact Email: <input type='text' name='settings-siteemail' /><br />  
  21.         </p>  
  22.         <p>  
  23.             <h2>Admin User Details:</h2>  
  24.             Admin Username: <input type='text' name='admin-username' /><br />  
  25.             Admin Displayname: <input type='text' name='admin-name' /><br />  
  26.             Admin Password: <input type='text' name='admin-pass1' /><br />  
  27.             Admin Password Again: <input type='text' name='admin-pass2' /><br />  
  28.         </p>  
  29.         <p style='border-top:1px solid #c6c6c6; padding-top: 10px;' >  
  30.             <input type='submit' value='Install!' />  
  31.             <input type='reset' value='Start Again!' />  
  32.         </p>  
  33.     </form>  
  34. </body>  
  35. </html>  

 

It's time to PHPify!

Now we can move on to the PHP section. Before we do this, let's get the logic down on what we're actually going to be doing in the PHP - and then we can just bash it out & hopefully have a fully working system!

  1. Check if any fields are blank - error if they are.
  2. Test the SQL Connection - error if no connection.
  3. Run the SQL - error if it fails.
  4. Insert the user details into users table - error if it fails.
  5. Insert the settings into settings table - error & rollback user detail insert if it fails.
  6. If we reached here it was a huge raging success - output success message.

A few things extra things to consider adding:

  • Check that the form is submitted via post
  • Store the SQL details (as I haven't written this in, there is no reason you can't compile a 'sql-connect.php' file on the fly.)
  • Delete the /install/ directory after successful installation.
  • Disallow use of the site until /install/ has been deleted.
  • Disallow install to run if there is already a users and/or settings table. Stops malicious reinstalls or whatever.

1. Check if any fields are blank - error if they are.

Php代码  收藏代码
  1. <?php  
  2. /* 
  3.     Let's set some variables + functions that we will use throughout! 
  4.  
  5.     error() - output error in nicely formatted box. 
  6. */  
  7. function error($msg){  
  8.     die("<div style='font-family: helvetica; border: 1px solid; padding: 10px; color: #D8000C; background: #FFBABA;'><strong>An Error Occurred:</strong><br />{$msg}</div>");  
  9.   
  10. }  
  11.   
  12. /* 
  13.     Check that none of the user inputs are blank 
  14.     If they are, then set error to true & break out the loop 
  15.     Check that the two entered passwords match 
  16. */  
  17. $error = false;  
  18. foreach($_POST as $key => $item){  
  19.     if(trim($item) == ""){  
  20.         $error = true;  
  21.         break;  
  22.     }  
  23. }  
  24.   
  25. if($error)  
  26.     error("All fields are required, please ensure they are all entered.");  
  27.   
  28. if($_POST['admin-pass1'] != $_POST['admin-pass2'])  
  29.     error("The two admin passwords do not match");  
  30. ?>  

 

From this point forward, assume that all PHP follows that already posted before.

 

2. Test the SQL Connection - error if no connection.

Php代码  收藏代码
  1. *  
  2.     Try to connec to SQL - if it returns false, we know it failed.  
  3. */  
  4. $connect = mysql_connect($_POST['sql-host'], $_POST['sql-username'], $_POST['sql-password']);  
  5. if(!$connect)  
  6.     error("The SQL host, username and password combination failed. Please try again");  
  7.   
  8.   
  9. /* 
  10.     Try to select database - if it returns false, we know it failed. 
  11. */  
  12. $database = mysql_select_db($_POST['sql-database'], $connect);  
  13. if(!$database)  
  14.     error("Unable to connect to database {$_POST['sql-database']}, please check it exists & try again.");  

 

3. Run the SQL - error if it fails.

Php代码  收藏代码
  1. /* 
  2.     Define what the SQL is that we need to run.  
  3.     Run the SQL  
  4.     If it returns false, we know that it has failed 
  5. */  
  6. $sql = <<<SQL  
  7. CREATE TABLE `users` (  
  8.     `user_id` int(11) auto_increment,  
  9.     `username` varchar(30),   
  10.     `display_name` varchar(50),  
  11.     `password` varchar (40),  
  12.     `admin` int(1),  
  13.     PRIMARY KEY (`user_id`)  
  14. );  
  15. CREATE TABLE `settings` (  
  16.     `setting_id` int(11) auto_increment,  
  17.     `setting_name` varchar(30),   
  18.     `setting_value` varchar(100)  
  19.     PRIMARY KEY (`setting_id`)  
  20. );  
  21. SQL;  
  22.   
  23. $sql = mysql_query($sql$connect);  
  24. if(!$sql)  
  25.     error("Creating tables failed: " . mysql_error());  

 

4. Insert the user details into users table - error if it fails.

Php代码  收藏代码
  1. /*  
  2.     Insert the User Details into `users` 
  3.     Compile SQL & Run - If it returns false we know it fails 
  4. */  
  5. $sql = "INSERT INTO `users` (`username`, `display_name`, `password`, `admin`)\n" .   
  6.     "VALUES ('{$_POST['admin-user']}', '{$_POST['admin-name']}', '" . sha1($_POST['admin-pass1']) . "''1')";  
  7.   
  8. $sql = mysql_query($sql$connect);  
  9. if(!$sql)  
  10.     error("Unable to insert admin user details into user database: " . mysql_error());  

 

5. Insert the settings into settings table - error & rollback user detail insert if it fails.

Php代码  收藏代码
  1. /*  
  2.     Insert the Settings into `settings` 
  3.     Compile SQL & Run - If it returns false we know it fails 
  4.     Delete the user from the `users` table & display error. 
  5. */  
  6. $sql = "INSERT INTO `settings` (`setting_name`, `setting_value`)\n" .   
  7.     "VALUES ('sitename', '{$_POST['settings-sitename']}'), \n" .   
  8.     "('sitetagline', '{$_POST['settings-sitetagline']}'), \n" .  
  9.     "('siteemail', '{$_POST['settings-siteemail']}')";  
  10.   
  11. $sql = mysql_query($sql$connect);  
  12. if(!$sql){  
  13.     mysql_query("DELETE FROM `users` WHERE `user_id` = '1'");   
  14.     error("Unable to insert site settings into user database: " . mysql_error());  
  15. }  

 

6. If we reached here it was a huge raging success - output success message.

Php代码  收藏代码
  1. echo "Wooo! Your site is successfully installed! You can now go and play with it!";  

 

Thanks for reading

So that's the ultimate basics of how to make one of those 'installer' things. There are obviously hundreds of ways that you can improve on what I've written, and as always, it's offered simply as a base for you to build off.

Remember these things:

  • The mysql_ functions should be replaced for mysqli(), I didn't merely demonstrative purposes.
  • You should make sure that you sanitize anything that you input into your database. It's not as critical here as no one will SQL inject their own site, however it's only a few extra words!

Let me know your thoughts, and if you think it's super awesome - why not share it with others, like it, or comment!

 

来源:http://forrst.com/posts/How_to_Write_a_PHP_MySQL_Install_Wizard-PUc

 

模块检测的方法

phpinfo() 转 数组

 

Php代码  收藏代码
  1. function phpinfo_array($return=false)  
  2. {  
  3.     /* Andale!  Andale!  Yee-Hah! */  
  4.     ob_start();  
  5.     phpinfo(-1);  
  6.    
  7.     $pi = preg_replace(  
  8.     array('#^.*<body>(.*)</body>.*$#ms''#<h2>PHP License</h2>.*$#ms',  
  9.      '#<h1>Configuration</h1>#',  "#\r?\n#""#</(h1|h2|h3|tr)>#"'# +<#',  
  10.      "#[ \t]+#"'#&nbsp;#''#  +#''# class=".*?"#''%&#039;%',  
  11.       '#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'  
  12.       .'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',  
  13.       '#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',  
  14.       '#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',  
  15.       "# +#"'#<tr>#''#</tr>#'),  
  16.     array('$1''''''''</$1>' . "\n"'<'' '' '' '''' ',  
  17.       '<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.  
  18.       "\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',  
  19.       '<tr><td>PHP Credits Egg</td><td>$1</td></tr>',  
  20.       '<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" .  
  21.       '<tr><td>Zend Egg</td><td>$1</td></tr>'' ''%S%''%E%'),  
  22.     ob_get_clean());  
  23.   
  24.     $sections = explode('<h2>'strip_tags($pi'<h2><th><td>'));  
  25.     unset($sections[0]);  
  26.   
  27.     $pi = array();  
  28.     foreach($sections as $section)  
  29.     {  
  30.         $n = substr($section, 0, strpos($section'</h2>'));  
  31.         preg_match_all('#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',$section$askapache, PREG_SET_ORDER);  
  32.         foreach($askapache as $m$pi[$n][$m[1]]=(!isset($m[3])||$m[2]==$m[3])?$m[2]:array_slice($m,2);  
  33.     }  
  34.   
  35.     return ($return === false) ? print_r($pi) : $pi;  
  36. }  
 

 

单独:

Php代码  收藏代码
  1. print_r(get_loaded_extensions());  
  2. //print_r(apache_get_modules());  
  3.   
  4. print_r(PHP_VERSION)  

 

参考:http://www.php.net/manual/en/function.phpinfo.php#87463

发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览