2017-11-15 1 views
0

문의 양식과 제품 양식이 있습니다. 제품 유형에만 제품 가치가 있는지 확인하고 싶지만 조건부를 통과 할 수 있습니다.

깨진 코드

// If visitor filled out the form on the "Contact Us" page (/contact/index.php) then no 'product' field is required. 
if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || "http://url.com/contact/index.php") { 
    if(strlen($product) < 2) { 
     $errors[] = "<font color='red'>Please enter the product requesting.</font>"; 
    } 
} 

문제 해결/디버깅 코드

$serverValue = $_SERVER['HTTP_REFERER']; 
print "The value of SERVER is ". $serverValue; 
echo "<br />"; 
print $_SERVER['DOCUMENT_ROOT']."/contact/index.php";    
echo "<br />"; 

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || "http://url.com/contact/index.php" || "/home/url/public_html/contact/index.php") { 
//if ($_SERVER['HTTP_REFERER'] != $_SERVER['DOCUMENT_ROOT']."/contact/index.php") { 
print "This is NOT the Contact page"; 
} else { 
print "This IS the Contact page"; 
} 

문제 해결/디버깅 출력

서버의 값은 http://url.com/contact/index.php입니다/home/url/public_html/contact/index.php 이는 연락처 페이지

당신은 올바른 HTTP_REFERER가 전달되는하지만 그냥 제대로 평가하지 않습니다 출력에 의해 볼 수 있습니다 아닙니다. 거기에 내가 다른 것들을 시도하고있는 주석 처리 된 라인이 있습니다. 나에게 쉽게 가보 라. PHP에 새로 온 것.

좋아, 나는 내가 잘못하고 있었는지 이해없이 성공

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || $_SERVER['HTTP_REFERER'] != "http://url.com/contact/index.php") { 
if(strlen($product) < 2) { 
$errors[] = "<font color='red'>Please enter the product requesting.</font>"; 
} 
} 

다른 생각으로이 시도?

+0

이 작업을 수행하면 –

답변

1

비어 있지 않은 문자열로 올바르게 평가됩니다 (true).

그래서 함께 :

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" 
    || true 
    || true) { 

결과 :

if (true) { 

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" 
    || "http://url.com/contact/index.php" 
    || "/home/url/public_html/contact/index.php") { 

는 두 번째와 세 번째는 항상 true로 평가하기 때문에 첫 번째 조건이 무엇인지는 중요하지 않습니다

리퍼러가이 3 개의 문자열 중 하나가 아니길 원한다면, 뭔가를 사용할 수 있습니다 g처럼 :

if (!in_array($_SERVER['HTTP_REFERER'] , [ 
    'http://www.url.com/contact/index.php', 
    'http://url.com/contact/index.php', 
    '/home/url/public_html/contact/index.php' 
]) { 
+0

을 사용할 수 없습니다. 감사 – Gots2Know

관련 문제