You are viewing revision #5 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
Here you learn to create and set a simple ahref-link to logout the current user.
The Logout in the CMenu Widget ¶
The starting Yii Web Application comes with the main-view located under /protected/views/layouts/main.php and the CMenu Widget in the mainmenu div:
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('post/index')),
array('label'=>'About', 'url'=>array('site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('site/contact')),
array('label'=>'Login',
'url'=>array('site/login'),
'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')',
'url'=>array('site/logout'),
'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
</div><!-- mainmenu -->
The Logout Link ¶
If you wanted your Logout-Link or -Button somewhere else, than in the Widget-Menu, create a link like the following:
<a href="<?php echo Yii::app()->createAbsoluteUrl('site/logout'); ?>">Logout</a>
The route goes to the actionLogout()-Method of your SiteController. Your current user will logout on clicking the link and get redirected to your projects homeUrl.
public function actionLogout() {
Yii::app()->user->logout();
$this->redirect(Yii::app()->homeUrl);
}
Show Logout only to logged-in users ¶
Just wrap an if arround your code and ask if the user is not a guest:
<?php if (!Yii::app()->user->isGuest){
<a href="<?php echo Yii::app()->createAbsoluteUrl('site/logout'); ?>">Logout</a>
}?>
Logout link update ,
u can create any random logout button using :
Html::a(
echo 'Sign out', ['/site/logout'], ['data-method' => 'post', 'class' => 'btn btn-default btn-flat']
)
Mine isn't that simple but it always works
<?php include 'connection.php'; //start of checking if user is logged in code if (!valid_credentials) { header('Location: login.php'); exit(); } $_SESSION['user'] = 'username'; if (!isset($_SESSION['user'])) { header('Location: login.php'); exit(); } //end of logged in code and starting a session $query = "SELECT * FROM people"; $result = mysql_query($query); While($person = mysql_fetch_array($result)) { echo "<h3>" . $person['Name'] . "</h3>"; echo "<p>" . $person['Description'] . "</p>"; echo "<a href=\"modify.php?id=" . $person['ID']. "\">Modify User</a>"; echo "<span> </span>"; echo "<a href=\"delete.php?id=" . $person['ID']. "\">Delete User</a>"; } ?> <h1>Create a User</h1> <form action="create.php" method="post"> Name<input type ="text" name="inputName" value="" /><br /> Description<input type ="text" name="inputDesc" value="" /> <br /> <input type="submit" name="submit" /> </form>
That helped me to raise money from a few college essay editing services
public function actionLogout() {
Yii::app()->user->logout(); $this->redirect(Yii::app()->homeUrl);
}
This calling funct() really help me out. I have use this to my ace my assignment, a web based project.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.