我有一个短代碼,可以"包裝"
<pre>
和
<code>
密碼陣列中的內容.這是前一段時間建立的,我在努力向该設置添加cookie或某種会话處理程式,以使具有密碼的使用者不必在特定時間內每次都重新輸入密碼.時間範圍。
截至目前,使用者必须在每个帖子上輸入密碼才能查看內容。
我已经調查了
setcookie
但我不明白如何將其与我正在使用的代碼合並(代碼不是我編寫的,開發人員已经消失了).我只是该站點的CSS設計师,試圖从中理解這一點。
I understand that it should be something like this?
$cookie_name = "mycookie";
$cookie_value = $result;
setcookie($cookie_name, $cookie_value,time()+3600*24,COOKIEPATH, COOKIE_DOMAIN);
This is the full code that's currently in use:
add_filter( 'the_content', 'wrap_code_in_shortcode' , 9 );
function wrap_code_in_shortcode($content) {
$content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[protected_content]$1", $content);
$content = preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/protected_content]", $content);
return $content;
}
add_shortcode( 'protected_content', 'protected_password_content' );
function protected_password_content( $atts, $content=null ) {
if (in_array(@$_REQUEST['password'], array('password'))){
$return = do_shortcode($content);
} else {
$return = '
<span>To view this section, enter your access code.</span><br>
<form action="" method="post">
<input style="display:block; width: 69%; height: 50px; margin-right: 1%; padding: 0px; float: left; border:1px solid blue;border-radius:3px;" type="text" placeholder="   Access Code Here" name="password">
<input style="display:block; margin: 0px; width: 30%; height: 50px; padding: 0px;" type="submit" value="Show Content">
</form>';
}
return $return;
}
最新回復
- 11月前1 #
欢迎来到wordPress開發StackExchange網站! 通常,您了解所有內容,但是有一个重要警告:如果某些頁面內容(如頁面標题,匯航選單等)已發送给使用者,則尝試从PHP指令碼設置cookie会产生以下錯誤:
"Cannot modify header information – headers already sent by..."
.為了避免這種情况,您可以在表單中添加一些javascript並通過此javascript設置cookie.让我们尝試以下代碼:您不需要指定Cookie的過期時間.如果您省略该部分,則该cookie將被設置為所謂的会话cookie,该会话cookie仅在使用者關闭瀏覽器之前才有效: