PHP ๊ธฐ์ด
PHP์ ์๋ฆฌ
- ์น๋ธ๋ผ์ฐ์ ์์ ์น ์๋ฒ๋ก
index.php์ ๋ฌ๋ผ๊ณ ์์ฒญ๋ณด๋ - ์น์๋ฒ๋
index.php์ ์ฒ๋ฆฌํ ์ ์๋ ํ๋ก๊ทธ๋จ์ธ php์๊ฒ ์์ํจ - php๋
index.php๋ฅผ ์ฐพ๊ณ , ํด์ํด์ html ํ์ผ์ ๋ง๋ฌ. - ์น ์๋ฒ๋ html์ ๋ฐ๊ณ ์น๋ธ๋ผ์ฐ์ ๋ก ๋ณด๋ด์ค.
PHP์ ๋ฐ์ดํฐ ํ์
์ซ์์ ์ฐ์ ์ฐ์ฐ์
- int : ์ ์ํ
- float : ์์ํ
๋ฌธ์์ด๊ณผ ๋ฌธ์์ด ์ฒ๋ฆฌ
- string
- โ
- ""
- ์ฐ์ฐ์
- . : ๋ฌธ์์ด ๋ถ์ด๊ธฐ ex) โhello โ.โworldโ โ โhello worldโ
๋ณ์
$๋ณ์๋ช์ผ๋ก ์ ์ธ
PHP์ URL ์ฟผ๋ฆฌ
php ์
๋ ฅ์ผ๋ก URL ์ฟผ๋ฆฌ์ ์ ๊ทผํ ๋๋ $_GET['key']๋ก ์ ๊ทผํ๋ค.
ex)
/index.php?name=shin
โ <?php echo $_GET['name']; ?>
โ shin ์ถ๋ ฅ
PHP ํจ์์ ์ฌ์ฉ
php์๋ ๋ง์ ๋ด์ฅํจ์๊ฐ ์์. ๋ํ์ ์ผ๋ก๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ์๋ ค์ฃผ๋ strlen(), ์์ค์ <br>๋ก ๋ฐ๊ฟ์ฃผ๋ nl2br
๋ฐ๋ณต๋ฌธ
๋ฐฐ์ด์ ํ์
array() ํจ์๋ก ๋ฐฐ์ด์ ๋ง๋ ๋ค.
ํค ์์ด ์์ฑํ๋ฉด ์ธ๋ฑ์ค๋ก ์ ๊ทผ๊ฐ๋ฅํ๊ณ , ํค๋ฅผ ํจ๊ป ์ ์ธํ๋ฉด ํค๋ก ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
<?php
$cars=array("volvo", "bmw");
echo $cars[1];
?>
<?php
$cars_with_key = array(
'aaa' => "volvo");
echo $cars['aaa'];
?>์น ์ดํ๋ฆฌ์ผ์ด์
form๊ณผ post
FORM์ action์ผ๋ก php๋ฅผ ์ฃผ์ด ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ์ ์๊ณ , POST๋ก ๋๊ธด ๋ฐ์ดํฐ๋ $_POST['key']๋ก ์ ๊ทผํ ์ ์๋ค.
<!doctype html>
<html>
<body>
<form action="form.php" method="post">
<p><input type="text" name="title" placeholder="Title"></p>
<p><textarea name="description"></textarea></p>
<p><input type="submit"></p>
</form>
</body>
</html>
// form.php
<?php
file_put_contents('data/'.$_POST['title'], $_POST['description']);
?>๊ธ ์์ ๊ธฐ๋ฅ ๊ตฌํ
php๋ html ํ๊ทธ ์ค๊ฐ์๋ ์ฝ์ ์ด ๊ฐ๋ฅํ๋ค. ์ด๋ ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ์๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค. ์ด๋ฅผ ํตํด input ํ๊ทธ์ ์์ ํ ๊ฐ์ ๋ํดํธ ๊ฐ์ผ๋ก ์ง์ ์ด ๊ฐ๋ฅํ๋ค.
<form action="update_process.php" method="post">
<input type="hidden" name="old_title" value="<?=$_GET['id']?>">
<p>
<input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>">
</p>
<p>
<textarea name="description" placeholder="Description">
<?php print_description(); >
</textarea>
</p>
<p>
<input type="submit">
</p>
</form>ํ์ผ๋ช
๋ณ๊ฒฝ์ rename() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ฅํ๋ค.
<?php
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
file_put_contents('data/'.$_POST['title'], $_POST['description']);
header('Location: /index.php?id='.$_POST['title']);
?>ํ์ํ ๊ฒฝ์ฐ์๋ง html ํ๊ทธ๋ฅผ ๋ ธ์ถ ์ํค๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค. ์๋ฅผ๋ค์ด ์๋ ์ฝ๋๋ URL์ id ์ฟผ๋ฆฌ๊ฐ์ด ์์ด์ผ๋ง update ๋งํฌ๋ฅผ ๋ณด์ด๋๋ก ํ ์์์ด๋ค.
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<?php } ?>๋ง๋ฌด๋ฆฌ
ํ์ผ๋ก ๋ชจ๋ํ - require
require(): ํ์ํ ํ์ผ์ ๋ถ๋ฌ์ด. ํ์ผ์ด ์์ ์ fatal error๊ฐ ๋ํ๋๋ค.require_once(): require()์ ๋์ผํ์ง๋ง, ์ฌ๋ฌ ๋ชจ๋์ ๊ฑฐ์ณ ํ๋ฒ๋ง ๋ถ๋ฌ์ด.include(): ํ์ํ ํ์ผ์ ๋ถ๋ฌ์ด. ํ์ผ์ด ์์ ์ warning์ด ๋ํ๋์ง๋ง ์คํฌ๋ฆฝํธ๊ฐ ๊ณ์ ์คํ๋จ.include_once(): include()์ ๋์ผ. ์ฌ๋ฌ ๋ชจ๋์ ๊ฑฐ์ณ ํ๋ฒ๋ง ๋ถ๋ฌ์ด.
๋ณด์
XSS
htmlspecialchars(): ๋ด๋ถ์ ์คํฌ๋ฆฝํธ ์ฝ๋๊ฐ ์์ด๋ ๊ทธ๋๋ก ๋ฌธ์์ด๋ก ์ถ๋ ฅํจ.strip_tags(): allowable_tags๋ก ํ์ฉํ ํ๊ทธ๋ง ๋จ๊ธฐ๊ณ , ๋๋จธ์ง ํ๊ทธ๋ ์ ๋ถ ์ง์ด ํ ์ถ๋ ฅ
ํ์ผ๊ฒฝ๋ก๋ณดํธ
basename():.,..์ ๊ฐ์ ์๋๊ฒฝ๋ก๋ฅผ ์ง์ฐ๊ณ , ํ์ผ๋ช ๋ง ๋จ๊ธฐ๋ ํจ์- ex)
../password.txtโpassword.txt
- ex)