php의 원리
php의 데이터 타입
숫자
<!-doctype html>
<html>
<body>
<h2>1+1</h2>
<?php
echo 1+1;
?>
<h2>2-1</h2>
<?php
echo 2-1;
?>
<h2>2*2</h2>
<?php
echo 2*2;
?>
<h2>4/2</h2>
<?php
echo 4/2;
?>
</body>
</html>
문자열
<!doctype html>
<html>
<body>
<h1>String & String Operator</h1>
<?php
echo "Hello \"w\"ord";
?>
<h2>concatenation operator</h2>
<?php
echo "Hello "."world";
?>
<h2>String length function</h2>
<?php
echo strlen("Hello world");
?>
</body>
</html>
변수
자바스크립트와 원리는 비슷하다.
<!DOCTYPE html>
<html>
<body>
<h1>Variable</h1>
<?php
$name = "leezche";
echo "Lorem ipsum dolor sit amet, consectetur ".$name." adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco ".$name." laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore egoing eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. by ".$name;
?>
</body>
</html>
URL 파라미터
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
안녕하세요. <?php echo $_GET['address']; ?>에 사시는 <?php echo $_GET['name']; ?>님
</body>
</html>
조건문
c언어와 원리는 비슷하다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Conditional</h1>
<h2>if</h2>
<?php
echo '1<br>';
if(false) {
echo '2-1<br>';
} else {
echo '2-2<br>';
}
echo '3<br>';
?>
</body>
</html>
반복문
c언어와 원리는 비슷하다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Loop</title>
</head>
<body>
<h1>while</h1>
<?php
echo '1<br>';
$i = 0;
while($i < 3){
echo '2<br>';
$i = $i + 1;
}
echo '3<br>';
?>
</body>
</html>
함수
function basic(){
print("Lorem ipsum dolor1<br>");
print("Lorem ipsum dolor2<br>");
}
basic();
function sum2($left, $right){
return $left+$right;
}
print(sum2(2,4));
file_put_contents('result.txt', sum2(2,4));
form과 post
게시물 작성 원리
form.html
<!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']);
?>
글 생성/수정/삭제 기능 구현하기
index.php
메인페이지이다.
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a> <!--글생성-->
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a> <!--글수정-->
<form action="delete_process.php" method="post"> <!--글삭제-->
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="delete">
</form>
<?php } ?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
</body>
</html>
create.php
글 생성 페이지이다.
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<form action="create_process.php" method="post"> <!--아래의 작성한 글 내용을 넘겨준다.-->
<p>
<input type="text" name="title" placeholder="Title">
</p>
<p>
<textarea name="description" placeholder="Description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
create_process.php
<?php
file_put_contents('data/'.$_POST['title'], $_POST['description']); /*제목.내용을 추가*/
header('Location: /index.php?id='.$_POST['title']); /*글을 생성한 페이지(id참조)로 이동*/
?>
update.php
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a> <!--id을 전달하여 글을 선택하여 수정가능*/
<?php } ?>
<h2>
<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>
</body>
</html>
update_process.php
<?php
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']); /*제목 바꾸기*/
file_put_contents('data/'.$_POST['title'], $_POST['description']); /*콘텐츠 다시 업데이트*/
header('Location: /index.php?id='.$_POST['title']); /*이전 페이지 이동*/
?>
delete_process.php
<?php
unlink('data/'.$_POST['id']); /*파일을 지움*/
header('Location: /index.php'); /*메인페이지로 이동*/
?>
모듈화
위 페이지들을 보면 동일한 구문이 상단에 반복되어 있어 관리가 어려움을 알 수 있다. 따라서 동일 구문을 하나의 파일로 따로 뺀후에 상단에 아래와 같이 작성해줌으로써 수정을 용이하게 할 수 있다.
<?php
require_once('lib/print.php');
?>
php 구문 뿐 아니라 html 구문 또한 모듈화 할 수 있다.
보안
XSS (Cross Site Scripting)의 위험이 있기 때문에 php에서 간단하게 제공하여 주는 함수가 존재한다.
첫번째 함수는 htmlspecialchars()로 모든 <등과 같은 구문들을 바꾸어서 태그를 이용하지 못하게 해준다.
아래와 같이 사용자가 입력한 값들에 모두 이 함수를 적용시켜주면 1차적으로 위험을 방지할 수 있다.
htmlspecialchars($_GET['id']);
두번째 함수는 basename()이라는 함수로 파일의 경로를 숨겨주는 함수이다.
부모 디렉터리를 알아내 파일의 구체적 위치를 파악해서 침입하는 경우를 방지할 수 있다.
basename($_GET['id']);
API
애플리케이션을 기반이 되는 시스템이 제공하는 기능을 사용하기 위해 이용하는 명령어.
'정리 > Web dev.' 카테고리의 다른 글
[web] php & mysql (0) | 2023.11.12 |
---|---|
[web] Cookie / Session (0) | 2023.05.14 |
[web] 웹 기본 상식 (0) | 2023.05.07 |
[web] javascript 총정리 (0) | 2023.04.02 |
[web] html & internet (0) | 2023.03.26 |
댓글