顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2016年2月9日 星期二

php web 的一些基本研究

寫任何的程式....基本上都會有hello world.....

沒錯...用php來寫 web當然也要hello world....

開啟hello.php.....

路徑放在你的web server 的目錄下.....如果以 apache2 server來說...就是

/var/www/html/

填入以下的內容

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>


用web browser
 http://localhost/hello.php or http://127.0.0.1/hello.php

如果你有看到下面的Hello World...恭喜你....你成功了...



其實你的web browser接收到的是


<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>


在<p>Hello World</p>
後面加入 <?php phpinfo(); ?>

就可以看到php 的 information.....




秀出變數的方法....
<?phpecho $_SERVER['HTTP_USER_AGENT'];?>


In the above example we printed an Array element
$_SERVER is just one variable that PHP automatically makes available to you

Example #2 Example using control structures and functions

<?phpif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
    echo 
'You are using Internet Explorer.<br />';}

else{
    echo 'You are not using Internet Explorer.<br />';
}
?>


 strpos() is a function built into PHP which searches a string for another string

Example #3 Mixing both HTML and PHP modes

<?phpif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {?><h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>
<?php} else {?><h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>
<?php}?>



Dealing with Forms


<form action="action.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

action.php 的內容如下:
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.



按下"提交"


echo htmlspecialchars($_POST['name']);

     htmlspecchars ->  轉型成字串的type ....   $_POST['name'] -> input type="text" name="name" 的變數內容
 

echo (int)$_POST['age'];

    (int) -> 轉型成int 的type...      $_POST['age'] -> input type="text" name="age"


Using old code with new versions of PHP

Two of the most important recent changes that affect old code are:

  • The old $HTTP_*_VARS arrays are not available as of PHP 5.4.0. The following superglobal arrays were introduced in PHP » 4.1.0. They are: $_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, $_REQUEST, and $_SESSION.
  • External variables are no longer registered in the global scope by default. In other words, as of PHP » 4.2.0 the PHP directive register_globals is off by default in php.ini. The preferred method of accessing these values is via the superglobal arrays mentioned above. Older scripts, books, and tutorials may rely on this directive being on. If it were on, for example, one could use $id from the URL http://www.example.com/foo.php?id=42. Whether on or off, $_GET['id'] is available.

What's next?

http://talks.php.net/


中文字的substr 

$text = $_POST['name'];
$text1 = mb_substr($text,0,1);
$text2 = mb_substr($text,1,1);

$text3 = mb_substr($text,2,1);




PHP 5 Form Validation






FieldValidation Rules
NameRequired. + Must only contain letters and whitespace
E-mailRequired. + Must contain a valid email address (with @ and .)
WebsiteOptional. If present, it must contain a valid URL
CommentOptional. Multi-line input field (textarea)
GenderRequired. Must select one


Text Fields

The name, email, and website fields are text input elements, and the comment field is a textarea. The HTML code looks like this:
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
Website: <input type="text" name="website">
Comment: <textarea name="comment" rows="5" cols="40"></textarea>

Radio Buttons

The gender fields are radio buttons and the HTML code looks like this:
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male

The Form Element

The HTML code of the form looks like this:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Big Note on PHP Form Security

The $_SERVER["PHP_SELF"] variable can be used by hackers!
If PHP_SELF is used in your page then a user can enter a slash (/) and then some Cross Site Scripting (XSS) commands to execute.
NoteCross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users.
Assume we have the following form in a page named "test_form.php":
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Now, if a user enters the normal URL in the address bar like "http://www.example.com/test_form.php", the above code will be translated to:

<form method="post" action="test_form.php">


Validate Form Data With PHP

The first thing we will do is to pass all variables through PHP's htmlspecialchars() function.
When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:
<script>location.href('http://www.hacked.com')</script>
- this would not be executed, because it would be saved as HTML escaped code, like this:
&lt;script&gt;location.href('http://www.hacked.com')&lt;/script&gt;
The code is now safe to be displayed on a page or inside an e-mail.
We will also do two more things when the user submits the form:
  1. Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function)
  2. Remove backslashes (\) from the user input data (with the PHP stripslashes() function)
The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over again).
We will name the function test_input().

Now, we can check each $_POST variable with the test_input() function, and the script looks like this:



Example Explained


<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>


<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name">
   <br><br>
   E-mail: <input type="text" name="email">
   <br><br>
   Website: <input type="text" name="website">
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>


</body>
</html>



Reference : http://www.w3schools.com/php/php_form_validation.asp Reference : http://php.net/manual/en/tutorial.php

2016年2月7日 星期日

php 的一些基本研究



執行  php file....

php   index.php



PHP 支援八種基本型態 (primitive type)

這八種分別是
  • 布林 (boolean)
  • 整數 (integer)
  • 浮點數 (floating point number)
  • 字串 (string)
  • 陣列 (array)
  • 物件 (object)
  • 資源 (resource)
  • NULL
基本上程式 (program) 會自動判斷變數 (variable) 的資料型態 (data type) , PHP 的變數寫法就比較特別一點,用金錢符號 $ 開頭,其後可接英文字母,如下 $b 為布林型態, $i 為整數型態, $f 為浮點數型態, $s 為字串型態

$b = True; // 布林型態
$i = 1234; // 整數型態
$f = 12.3; // 浮點數型態
$s = "12"; // 字串型態

陣列就比較複雜一點,因為陣列分成兩部份,第一部分為索引,第二部份則是資料,兩者可以是上面四者型態任一,資料還可以是其他的陣列,例如
$a1 = array(10, 20, 30, 40);
$a2 = array(
    False => True,
    33 => "Tony",
    "A" => 33.06,
    6.32 => array(1, 2),
);
建立陣列需要用到內建函數 (function) array() ,索引與資料放在參數列 (parameter list) ,若無提供索引,如 $a1 ,索引會自動由 0 遞增填補,因此存取 $a1 如下
echo $a1[0];
echo $a1[1];
echo $a1[2];
echo $a1[3];

來個  imagettftext 的例子....

由於他需要  PHP GD2 library

sudo apt-get install php5-gd



<?php// Set the content-typeheader('Content-Type: image/png');// Create the image$im = imagecreatetruecolor(400, 30);// Create some colors$white = imagecolorallocate($im, 255, 255, 255);$grey = imagecolorallocate($im, 128, 128, 128);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, 399, 29, $white);// The text to draw$text = 'Testing...';// Replace path by your own font path$font = 'arial.ttf';// Add some shadow to the textimagettftext($im, 20, 0, 11, 21, $grey, $font, $text);// Add the textimagettftext($im, 20, 0, 10, 20, $black, $font, $text);// Using imagepng() results in clearer text compared with imagejpeg()imagepng($im);imagedestroy($im);?>

改一下例子....

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 400);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 399, $white);

// The text to draw
//$text = 'Testing...';
$text = '紀富中';
// Replace path by your own font path
//$font = '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf';
$font1 = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc';
$font2 = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc';

// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 30, 0, 40, 40, $black, $font1, $text);
imagettftext($im, 30, 0, 80, 80, $black, $font2, $text);
imagepng($im);
imagedestroy($im);
?>

執行結果如下 :


還有一些可以畫圖畫線的library....

http://jpgraph.net/download/
http://pydoing.blogspot.tw/2012/11/php-How-to-Execute.html

一些字型下載的網址
http://fonts.mobanwang.com/