Jump to content

Simple Logout Link CSRF


noj75

Recommended Posts

Hi all,

 

I am creating an external page that pulls the member data from the database, that is no problem.

 

I need to create a logout link on this external page but am unable to do this as I do not know how to get the current (logged in) members CSRF Key.  So when I point the link to forum/logout/ I get the The CSRF protection key did not match, error page.

 

Does anyone know how I can get the CSRF key data to be able to log the user out please?

Kind regards

Link to comment
Share on other sites

@newbie LAC

 

EDIT: Sorry, I meant @Vikestart

The code below works fine for me.  Hopefully this will help you out.  Sorry for the delay.

 

<?php
/* ############### CALL THE IPS MEMBER INFO ############### */
require_once( 'path/to/your/init.php' );
\IPS\Session\Front::i();
$memCSRF = \IPS\Session::i()->csrfKey;
$member = \IPS\Member::loggedIn();
/* ### SET THE MEMBER VARIABLES ### */
$memName = $member->name;
$memEmail = $member->email;
$memID = $member->member_id;
/* ############### END IPS MEMBER INFO ############### */
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<p>Hello <?php echo $memName; ?></p>

<p><a href="forum/logout/?csrfKey=<?php echo $memCSRF; ?>">Logout</a></p>
<!-- ## CHANGE THE LINK TO YOUR LOGOUT PAGE ## -->

</body>
</html>

 

Link to comment
Share on other sites

Yeah, I know, I'm using the same code, but I'm getting two different keys :unsure:

The key I'm getting on my external page doesn't match the key generated in the IPS suite..

​Check your cookie settings

How to generate the key

		/* Set ID */
		$this->id = session_id();

		/* Crate csrf key */
		$this->csrfKey = md5( "{$this->member->email}&{$this->member->member_login_key}& " . ( $this->member->member_id ? $this->member->joined->getTimestamp() : 0 ) . '&' . $this->id );

I think you have 2 cookies prefix_IPSSessionFront. For site and for forum.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 years later...

I'm not sure how it works as of now I only learned about csrfKeys yesterday but noj75's code allows me to build login, logout and get the member's ID so I can query all the information I need from the database. Do you need help understanding the concept or putting the code (PHP) together? I was planning on learning more about csrfKeys today but I can help you put the code together if that is what you need.

Question guys, what is the best way to check for an active session? As of now I'm using:

$_SESSION['ips4_member_id']

Would you recommend another method? Thank you.

Edit:

Actually that's not working. I'm forced to use:

    if(isset($memID) && !empty($memID)){

    }
    else{

    }

I'm sure there's a better way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...