简体   繁体   中英

How to create a dynamic link?

I'm trying to create a link that takes the user to two different pages depending is the user logged or not. Problem is I'm still new to programming and this is quite big bite for beginner like me but its something I have to do. I created something like this so far but either way I suck at searching or there just isnt specific information for what I need

<?php if($userLogged){ 
 echo '<a href="index.php" class="stylelink">';
 } 
 else 
 { 
 echo '<a href="index1.php" class="stylelink">';
 } 
 echo "Etusivu</a>";
?>

I'm also using Dreamweaver's login function that creates the MM_Username session and such, and Im not sure how to make the condition. userLogged is still an empty variable. Id appreciate any advice.

Thanks -John

well, instead of using echo statements in the php tag you can write html and use php for outputting the value of the page like this

<a href="<?php echo (isset($_SESSION['MM_Username']))?'index.php':'index1.php';?>" class="stylelink">Etusivu</a>

The $_SESSION['MM_Username'] works if you have included session_start(); at the beginning of the page and you can use the condition as above instead of $userLogged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM