개발! 딱 깔끔하고 센스있게!

PHP: Notice: 정의되지 않은 색인으로 세션 변수를 정의했습니다 본문

개발 스크랩 메모/PHP

PHP: Notice: 정의되지 않은 색인으로 세션 변수를 정의했습니다

렉사이 2020. 12. 21. 22:42

나는 이메일 검증기로 등록 시스템을 만들고 있다.

당신의 전형적인 "이 코드 검증" 형식의 물건을 사용합니다.

나는 세션 변수를 저장하기를 희망한다.

이러한 사람들은 등록페이지에 계좌등록을 완수하고, 어떤 방식으로 이외의 페이지로 내비게이션을 가져올 때, 그들이 사용하기 전에 계좌를 활성화시킬 것을 깨우친다.

이 문제를 이처럼 진단하기 어려운 것은, 나는 비슷한 방식으로 많은 기타 세션 변수를 사용했지만, 이것은 근본적으로 작용하지 않는다는 것이다.

나의 방법은:

/* This is placed after the mail script and account creation within the same if statement. Things get executed after it, so I know it's placed correctly. */ $_SESSION['registrationComplete'] = TRUE; // I've tried integer 1 and 'Yes' as alternatives. 

지금 변수를 검사하기 위해서 나는 이것을 페이지의 맨 위에 두었다.

echo $_SESSION['registrationComplete']; // To see if it's setting. This gives the // undefined index notice. if (isset($_SESSION['registrationComplete'])) { // Alternatively, I have nested another if that simply tests if it's TRUE. echo $_SESSION['registrationComplete']; // When echo'd here, it displays nothing. echo '

Congratulations, Foo! Go to *link to Bar*.

'; }

현재, 나는 페이지를 새 페이지로 바꾸게 하였으나, 나는 그것을 꺼내 테스트했다.

페이지가 submit 에서 다시 불러올 때, 위 if 문구에 대한 메시지가 나타나고, 나는 알림: Undefined index: regicstrationComplete blah blah from the echoong of the sesssion var!

만약 내가 이 페이지로 다시 돌아간다면, 그것은 if 문구를 무시할 것이다.

나는 이미 입력 오류와 모든 것을 테스트하고 세션 변수를 제거하여 오래된 테스트 방해를 막기 위해 사용했으나 나는 운이 없다.

많은 google 은 단지 사람들이 이러한 잘못을 억제하는 것을 보여 주지만, 이것은 매우 미친 것 같다!뿐만 아니라, 내 사이트에서 다른 지역의 회화 변수의 지속성도 다르다.

누가 내가 뭘 잘못했다고 지적할 수 있을까?살려주세요!감사합니다.

참고로, 나는 관련 문제를 읽었기 때문에, 나도 초학자이기 때문에 어떤 건의를 이용할 수 있을지 모른다.

요구에 따라 더 많은 코드 를 대량 주석 하여 간결하게 유지 하다

var_dump($_SESSION); // It's here to analyze that index message. I guess it's not important. echo $_SESSION['registrationComplete']; if (isset($_SESSION['registrationComplete'])) { // The golden ticket! This is what I want to appear so badly. echo 'Congratulations, Foo! Go to *link to Bar*.'; } // Explanation: I don't want logged in users registering. // The else statement basically executes the main chunk of code. if (isset($_SESSION['user_id'])) { echo 'You are logged in as someone already.'; } else { if (isset($_POST['submitRegister'])) { // Code: Database connection and parsing variables from the form. if (!empty($email) && !empty($email2) && $email == $email2 && !empty($displayName) && !empty($password) && !empty($password2) && $password == $password2) { // Code: Query to retrieve data for comparison. if (mysqli_num_rows($registrationData) == 0) { // Code: Generates the salt and verification code. // Code: Password hashing and sending data to verify database. // E-mail the verification code. $_SESSION['registrationComplete'] = 'yes'; } else { // Some error handling is here. $registerError = 'The e-mail address you entered is already in use.'; } } // the elseif, elseif, and else are more error handling. elseif ($email != $email2) { $registerError = 'Your e-mails did not match'; } elseif ($password != $password2) { $registerError = 'Passwords didn\'t match.'; } else { $registerError = 'Filled out completely?'; } // If the registration was submitted, but had errors, this will print the form again. if (!isset($_SESSION['registrationComplete'])) { require_once REF_DIR . REF_REGISTERFORM; } // IMPORTANT! it turns out my code did not work, I forgot I had the same statement elsewhere. else { echo 'Congratulations, Foo! Go to *link to Bar*.'; } } // Creates form. else { require_once REF_DIR . REF_REGISTERFORM; } } 

대답

이것은 디버깅의 기초로 귀속할 수 있다.

  1. Understand as much as you can about the technique/library/function/whatever that you're trying to use.
  2. Inspect the salient bits and make sure that they are what you expect or what they should be. (There's a slight difference between those two, depending on the situation.)
  3. If that doesn't bring you towards a solution, step back and make sure you're understanding the situation. This may mean simplifying things so that you're only dealing with the issue at hand, i.e. create a separate, simpler test case which exposes the same problem. Or, it may simply mean that you stop coding and work through the flow of your code to make sure it is really doing what you think it is doing.

세션이 작용하지 않는 한 전형적인 문제는 세션 사용을 잊어버리는 어떤 페이지의 sessionu start() (가까이거나 꼭대기에 위치한 것이다.

내가 가장 좋아하는 PHP 코드필드 중 하나로 디버깅에 사용된다:

print '
'; var_dump($some_variable); print '
';

나는 print 를 사용하여 디버깅을 시도하고 echo 를 사용하여 일반 출력을 진행하려고 한다.

일단 코드 가 미미한 출력 을 초과하면 디버그 코드 를 발견하기 쉽다.

이와 함께 var dump 은 변수에 대한 정보를 더 많이 인쇄할 수 있으며, 변량의 유형과 크기를 비교합니다.

더 쉽게 출력을 읽기 쉽도록 포장하는 것이 중요하다.

Comments