php json 파일 수정 관련 질문

-@Naver의 이미지

간단한 회원가입을 수행하는 프로그램을 만들어보기위해서
json 파일에 id와 비밀번호를 저장하는 방식으로 진행하려합니다.
그런데 json 파일에 입력받은 값을 넣으면 왠진 모르겠지만 빈칸이 들어가는 형식이네요...??
어떤것이 문제일까요..? 프로그램은 json 파일이 없을때부터 작동 가능하도록 해야하고요. 이미 json 파일에 존재하는 아이디일 경우 이미 있는 아이디라고 사용자한테 알려주는 기능이 있습니다

메인화면 php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
  <h1>로그인 및 회원가입 하기</h1>
  <form action="sessionJoin.php" method="post">
    아이디 : <input type="text" name="id" id="id"><br />
    <br>
    비밀번호 : <input type="password" name="pw" id="pw"><br />
    <br>
    <input type="submit" value="로그인" onclick="location.href='sessionLogin.php'">
    <input type="submit" value="회원가입" onclick="location.href='sessionJoin.php'"></button>
    <button type="reset" name="button">초기화</button>
  </form>
</body>
</script>
</html>

SessionJoin.php

<?php
  $id = $_POST['id'];
  $password = $_POST['pw'];
  $result = array("id"=>$id,"pw"=>$password);
  $json_file = file_get_contents('data/person.json');
  $data_text = $json_file;
  $bool = true;
 
  if($data_text == null){
    while($data_text != null){
      $one = substr($data_text,strpos($data_text,"{"),strpos($data_text,"}")+1);
      $data_text = str_replace($one,"",$data_text);
      $str = substr($one,strpos($one,":")+2,strpos($one,",")-8);
 
      if($str == $id){
        echo "이미 등록된 아이디 입니다.";
        $bool = false;
        break;
      }
    }
  }
  if($bool){
    $format = '{"id":"%s","pw":"%s"}';
    $json_result = sprintf($format,$result['id'],$result['pw']);
    file_put_contents('data/person.json', $json_result , FILE_APPEND);
    print_r($json_result);
    echo "회원 가입이 완료되었습니다.";
  }
?>

json 파일이 없을때 수행 후 json 파일 모습
{"id":"","pw":""}{"id":"test111","pw":"aefaevgaa"}

그다음 수행 후 json 파일 모습
{"id":"","pw":""}{"id":"test111","pw":"aefaevgaa"}{"id":"","pw":""}{"id":"aefafe","pw":"aefdcaveg"}

swish95의 이미지

id 랑 pw 가 없을때 추가하지 않도록 하고 원래 값이랑 추가 값이람 보면 금방 알겁니다.

근데 json 을 문자열로 처리하다니요
그럴거면 json 을 쓸필요가 없습니다.
json 그대로 php 배열로 변환해서 사용하시고 마지막에 다시 json 형태로 바꿔야죠
json_decode, json_encode 함수를 쓰세요

------------------------------------------------------------
ProgrammingHolic

-@Naver의 이미지

음.. json을 물론 원래 사용하는 방식대로 사용할려 했습니다만.. 다만 json_decode 함수만을 사용했는데 이경우엔 format을 사용하지 않고 json_result에 json_decode를 사용하여 file_put_contents를 사용해 넣었습니다.
다만 이경우엔 id와 pw에 공백이 아닌 null로 들어가더군요..

swish95의 이미지

디버깅 해봐야 하겠지만 json_decode 했을때 문제가 없도록 하는게 맞지 싶습니다.

------------------------------------------------------------
ProgrammingHolic

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.