보안을 위해 register_global 값을 off 로 놓고 쓰다보니 기존 프로그램을 손봐줘야 할 일이 생겼다.
이전 소스에서 변수명만 교체해주면 바로 사용이 가능하다.
[cart.class]
<?
class Item {
var $name; //품목명
var $code; //DB용 코드
var $count; //수량
var $price; //단가
}
class Cart {
var $item;
function set($name,$code,$count,$price) //장바구니 넣기
{
$this->item[$name] = new Item;
$this->item[$name]->name=$name;
$this->item[$name]->code=$code;
$this->item[$name]->count=$count;
$this->item[$name]->price=$price;
}
function get($name) //품목명으로 내용 읽어오기
{
return $this->item[$name];
}
function fetch() //품목내용을 하나씩 가져오기
{
$temp=current($this->item);
if (!$temp) reset($this->item);
else {next($this->item);}
return $temp;
}
function remove($name) //품목을 삭제하기
{
unset($this->item[$name]);
}
}
?>
주의점 : class를 선언함에 있어서 만약 session()을 부여하고 싶다면 반드시 session_start() 보다 선행되야 하며 선행됨에 있어서 class 파일의 ?> 태그의 마무리엔 공백을 허용해선 안된다.
-> session cache limiter 에러가 발생한다.
[process.php]
<?
require("cart.class");
session_start();
// $_SESSION[cart] 값이 있는가?
if (!isset($_SESSION[cart])) {
echo"세션장바구니 객체가 없다<br>";
session_register("cart"); //cart을 세션객체로 등록
$_SESSION[cart] = new Cart;
}