2014-11-28 3 views
1

쿠키에서 $ dir으로 값 ($ login)을 전달하고 싶습니다. file.phpPHP 클래스의 쿠키에서 값을 가져 옵니까?

<?php 
class File { 
    public $filename; 
    public $login; 
    public $dir; 
    public function __construct() { 
     $this->login = $_COOKIE['login']; 
     this->dir = "userFiles/" . $this->login . "/"; 
     $createDir="./userFiles/".$login; 
     if (!is_dir($createDir)){ 
      mkdir($createDir,0777,true); 
     } 
     $action = isset($_POST['action']) ? $_POST['action'] : false; 
     $this->filename = isset($_POST['filename']) ? $_POST['filename'] : false; 
    } 
    private function save() { 
     $content = isset($_POST['content']) ? $_POST['content'] : ''; 
     file_put_contents($this->dir.$this->filename, urldecode($content)); 
    } 
} 
$file = new File(); 
?> 

이 전체 코드입니다. filename과 action 변수는 index.php 페이지에서 ajax에 의해 전달됩니다.

의 index.php이 당신을 위해 작동합니다

<?php 
    if (! isset($_COOKIE['login'])) { 
     echo "Need to log in first"; 
     header("refresh:2;url=login.php"); 
     exit; 
    } 
?> 
<!doctype html> 
<html> 
<head> 
<title>Editor</title> 
</head> 
<body> 
    <textarea id="code" name="code">code goes here</textarea> 
<br/><button id="save">save</button> 
<input type="text" id="filename" value="test.txt"><br> 
<script src="JavaScript/jquery.js"></script> 
<script> 
var url = 'file.php'; 
$("#save").click(function() { 
    $.ajax({ 
     url : url, 
     type: 'post', 
     xhrFields: { 
    withCredentials: true 
    }, 
     data : { 
      filename : $("#filename").val(), 
      action : 'save', 
      content : encodeURIComponent($('#code').val()) 
     } 
    }); 
}); 
</script> 
</body> 
</html> 
+0

당신은 두 번'$의 login'를 할당하려고하는 이유

class File { public $filename; public $login; //problem here public $dir; // $login is not getting here. It works if I remove $login. public function __construct() { $this->login = $_COOKIE['login']; $this->dir="userFiles/".$this->login."/"; } } $obj = new File(); echo $obj->login . "<br />"; echo $obj->dir; 

그래서 전체 코드의 모양은? 다음과 같이 login을 선언하면됩니다 :'public $ login' 또한 생성자에서 dir을 지정하십시오! – Rizier123

+0

표 : $ dir을 (를) 지정하는 곳에'$'기호를 잊어 버렸습니다! – Rizier123

+0

@ Rizier123 $ 기호를 추가했습니다. userFiles 폴더를 만들지 만 $ login 폴더를 안에 만들고 파일을 저장하지 않습니다. – zeee9

답변

3

이을에 데이터를 전송 메인 페이지입니다!

(당신 만 class definitionclass membersconstant values을 할당 할 수 있습니다 그래서 당신은 constructor 또는 function에 할당하려면 을 사용하여 클래스 변수에 액세스해야합니다.

예를 들어 다음을 참조

class File { 

    public $filename; 
    public $login; 
    public $dir; 

    public function __construct() { 
     $this->login = $_COOKIE['login']; 
     $this->dir = "userFiles/" . $this->login . "/"; 

     if (!is_dir($createDir)){ 
      mkdir($createDir,0777,true); 
     } 

    } 

} 

자세한 내용은 : http://php.net/manual/en/language.oop5.properties.php

+0

코드가 여전히 작동하지 않습니다. 위의 전체 코드를 포함했습니다. 나는 이것을 한 시간 이상 디버깅하려했기 때문에 도움을 요청하십시오. – zeee9

+0

@ zeee9 쿠키를 설정 하시겠습니까? 또한 그것을 테스트하기 위해 예제 (첫 번째 코드 상자)를 확장했습니다! 그것은 작동해야합니다. – Rizier123

+0

작동하지 않았습니다. 나는 index.php를 포함시켰다. – zeee9

관련 문제