PHP تساعد في إخفاء الملاحة مع ملف تعريف الارتباط

StackOverflow https://stackoverflow.com/questions/2870033

  •  03-10-2019
  •  | 
  •  

سؤال

لدي علامات التبويب هذه على التنقل الخاص بي:

<li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks")  echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings")  echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>   

وأريد فقط أن أظهر لهم عند تسجيل الدخول:

if ($_COOKIE['custid'] == "admin") {

echo "Customers";
echo "Trunks";
echo "Settings";

}

كيف يمكنني الجمع بين اثنين من هذه البرامج النصية ؟؟؟

هل كانت مفيدة؟

المحلول

<?php
if ($_COOKIE['custid'] == "admin") { ?>
<li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks")  echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings")  echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
<?php } ?>

بسيط جدا ، ضعه داخل الآخر ...

نصائح أخرى

التعامل مع قضية "المسؤول في ملف تعريف الارتباط" كقضية منفصلة ...

<?php if($admin): ?>
    <li<?php if ($thisPage=="Customers"): ?> class="current"<?php endif; ?>><a href="/customers/">Customers</a></li>
    <li<?php if ($thisPage=="Trunks"): ?> class="current"<?php endif; ?>><a href="/trunks/">Trunks</a></li>
    <li<?php if ($thisPage=="Settings"): ?> class="current"<?php endif; ?>><a href="/settings/">Settings</a></li>
<?php endif; ?>

بناء جملة PHP INLINE أجمل بكثير من استخدام {} و echos داخل HTML

لست متأكدا بالضبط ما تعنيه ، ولكن:

<?php
if ($_COOKIE['custid'] == "admin") { ?>
 <li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
 <li<?php if ($thisPage=="Trunks")  echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
 <li<?php if ($thisPage=="Settings")  echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
<?php } else { ?>
 <li><a href="/customers/">Customers</a></li>
 <li><a href="/trunks/">Trunks</a></li>
 <li><a href="/settings/">Settings</a></li>
<?php } ?>

// OR

<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Trunks")  echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Settings")  echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>

وأنا أتفق مع webDestroya في التعليقات على المنشور نفسه ؛ يجب عليك استخدام جلسة أو مماثلة بدلاً من ملف تعريف الارتباط للتحقق من حالة المسؤول. أنا فقط لم أغيره هنا من أجل المثال.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top