7.2與7.4的差異
PHP 7.4的效能比7.2好很多, 而且7.4支援 MySQL8.0 的 caching_sha2_password. 所以建議改為 7.4.
不過PHP 7.4套件只有在 ubnutu20.04版本以上才找得到. 所以也建議一定要安裝ubuntu20.04
安裝 php 7.4
執行下面指令即可
sudo apt-get -y install php7.4 libapache2-mod-php7.4 php7.4-mysql php7.4-curl php7.4-gd php7.4-intl php-imagick php7.4-imap php7.4-mbstring php7.4-common php-zip php7.4-xml
sudo systemctl restart apache2
測試
在/var/www/html之下新增info.php
<?php phpinfo(); ?>
存檔後, 更改擁有者為www-data
sudo chown www-data:www-data /var/www/html/info.php
啟動SSL
sudo a2enmod ssl sudo a2ensite default-ssl sudo systemctl restart apache2
安裝PhpAdmin
sudo apt-get install phpmyadmin cd /var/www/html sudo ln -s /usr/share/phpmyadmin sudo chown -R www-data:www-data .
測試
http://localhost/phpmyadmin
php.ini
ubuntu :
sudo vim /etc/php/7.4/apache2/php.ini : 注意, 是apache
debian :
sudo vim /etc/php/7.3/cli/php.ini : 注意, 是cli
upload_max_filesize=20M #修改最大上傳檔案大小
最後需重新啟動apache
sudo /etc/init.d/apache2 restart
PHP XML
PHP支援XML的寫法, 如下. 請注意 “XML” 必需置於整列最前面, 不可縮排. 另需外加套件
sudo apt-get install php7.4-xml
$data = <<<XML <info> <dbAccount>student</dbAccount> <dbPassword>123456</dbPassword> <dbName>mydb</dbName> </info> XML; $xml = simplexml_load_string($data); print($data);
Java讀取 xml
ParseXmlService.java, 此為解析xml 類別, 資料記錄於 HashMap物件中
package com.asuscomm.mahaljsp.freekingresume; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.io.InputStream; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class ParseXmlService { public HashMap<String, String> parseXml(InputStream inStream) throws Exception { HashMap<String, String> hashMap = new HashMap<String, String>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inStream); Element root = document.getDocumentElement(); NodeList childNodes = root.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = (Node) childNodes.item(j); if (childNode.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) childNode; String key=childElement.getNodeName(); if(!key.equals(""))hashMap.put(key, childElement.getFirstChild().getNodeValue()); } } return hashMap; } }
MainActivity.java
private void login(String vendor, String password){ new Thread(()->{ String urlString = String.format("%s/login.php?txtAccount=%s&txtPassword=%s",G.httpurl, vendor, password); InputStream inStream = null; try { inStream = new URL(urlString).openStream(); ParseXmlService service = new ParseXmlService(); HashMap<String, String>appInfoHashMap = service.parseXml(inStream); G.dbAccount=appInfoHashMap.get("dbAccount"); G.dbPassword=appInfoHashMap.get("dbPassword"); G.dbName=appInfoHashMap.get("dbName"); spEditor.putString("VENDOR",vendor); spEditor.putString("USERPASSWORD", password); spEditor.apply(); handler.sendEmptyMessage(SERVER_CONNECTED); } catch (MalformedURLException e) { Log.d("Thomas", "Malformed url :字串不是網址"); handler.sendEmptyMessage(APERROR); } catch (IOException e) { handler.sendEmptyMessage(NO_INTERNET); } catch (Exception e){ handler.sendEmptyMessage(APERROR); } }).start(); }