<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Is it possible this could be simplified somewhat by using
user_external_login()?<br>
<br>
<a class="moz-txt-link-freetext" href="http://api.drupal.org/api/function/user_external_login/6">http://api.drupal.org/api/function/user_external_login/6</a><br>
<br>
Ernst Plüss wrote:
<blockquote
cite="mid:27c534c91002080911u4716f38bt9300269d60443cec@mail.gmail.com"
type="cite">Hi drupal friends<br>
<br>
I've written a patch for the ip_login module. It extends the module
with the possiblity to have a "login as an other user" link.<br>
<br>
Basically it does the following things:<br>
<ol>
<li>Logs out the current user.</li>
<li>Makes sure ip_login does not straight login again.</li>
<li>Shows the user login screen.</li>
</ol>
My code works, but I'm not 100% sure whether it's save to code it like
that. Could someone have an I on it?<br>
<br>
<br>
<font face="monospace" size="4"><font color="#0000ff">/**</font><br>
<font color="#0000ff"> * Logs the current user out and start new
session.</font><br>
<font color="#0000ff"> *</font><br>
<font color="#0000ff"> * Most of the code taken from user_logout()
and _drupal_bootstrap().</font><br>
<font color="#0000ff"> */</font><br>
<font color="#a020f0">function</font> ip_login_as_different_user<font
color="#6a5acd">()</font> <font color="#6a5acd">{</font><br>
<font color="#2e8b57"><b>global</b></font> <font color="#804040"><b>$</b></font><font
color="#008080">user</font>;<br>
<br>
watchdog<font color="#6a5acd">(</font>'<font color="#ff00ff">user</font>',
'<font color="#ff00ff">Session closed for %name.</font>', <font
color="#2e8b57"><b>array</b></font><font color="#6a5acd">(</font>'<font
color="#ff00ff">%name</font>' <font color="#804040"><b>=</b></font><font
color="#804040"><b>></b></font> <font color="#804040"><b>$</b></font><font
color="#008080">user</font><font color="#2e8b57"><b>-></b></font><font
color="#008080">name</font><font color="#6a5acd">))</font>;<br>
<br>
<font color="#0000ff">// Destroy the current session:</font><br>
<font color="#008080">session_destroy</font><font color="#6a5acd">()</font>;<br>
<font color="#0000ff">// Only variables can be passed by reference
workaround.</font><br>
<font color="#804040"><b>$</b></font><font color="#008080">null</font> <font
color="#804040"><b>=</b></font> <font color="#2e8b57"><b>NULL</b></font>;<br>
user_module_invoke<font color="#6a5acd">(</font>'<font color="#ff00ff">logout</font>',
<font color="#804040"><b>$</b></font><font color="#008080">null</font>,
<font color="#804040"><b>$</b></font><font color="#008080">user</font><font
color="#6a5acd">)</font>;<br>
<br>
<font color="#0000ff">// Load the anonymous user</font><br>
<font color="#804040"><b>$</b></font><font color="#008080">user</font> <font
color="#804040"><b>=</b></font> drupal_anonymous_user<font
color="#6a5acd">()</font>;<br>
<br>
<font color="#a020f0">require_once</font> variable_get<font
color="#6a5acd">(</font>'<font color="#ff00ff">session_inc</font>', '<font
color="#ff00ff">./includes/session.inc</font>'<font color="#6a5acd">)</font>;<br>
<font color="#008080">session_set_save_handler</font><font
color="#6a5acd">(</font>'<font color="#ff00ff">sess_open</font>', '<font
color="#ff00ff">sess_close</font>', '<font color="#ff00ff">sess_read</font>',
'<font color="#ff00ff">sess_write</font>', '<font color="#ff00ff">sess_destroy_sid</font>',
'<font color="#ff00ff">sess_gc</font>'<font color="#6a5acd">)</font>;<br>
<font color="#008080">session_start</font><font color="#6a5acd">()</font>;<br>
<br>
<font color="#804040"><b>$</b></font><font color="#008080">_SESSION</font><font
color="#6a5acd">[</font>LOGIN_AS_DIFFERENT_USER<font color="#6a5acd">]</font> <font
color="#804040"><b>=</b></font> <font color="#ff00ff">TRUE</font>;<br>
<br>
<font color="#0000ff">// show the login page</font><br>
drupal_goto<font color="#6a5acd">(</font>'<font color="#ff00ff">user</font>'<font
color="#6a5acd">)</font>;<br>
<font color="#6a5acd">}</font></font><br>
<br>
Thanks for taking your time!<br>
Ernst<br>
<br>
</blockquote>
<br>
</body>
</html>