Solved

Adding domain info after username similar to Hotmail at SquirrelMail login page

Posted on 2003-11-04
20
2,127 Views
Last Modified: 2012-08-21
The title says it all I guess. I am not a JS guru. I am looking for a javascript script that would add the domain after the user name same as Hotmail does on their site. I am trying to modify the SquirrelMail webmail application's /src/login.php file so that a designated @domain.com can be inserted after user types his ID and moves to the password field. This is a requirement of the site's login process. The site is unique and the @... is specific and static, but if anyone can invent something better such as pulling the domain info from the address URL, that's great too.

Since the page has PHP, it may also be easier to add some PHP tags there, but I think JS would be the easiest way to go. If you want to see how Hotmail script works, look at the source in their login page.

Any working solution or workaround would help.

Since I have seen JS scripts implement similar to this with as little as 5 lines, I don't think this will be a majot effort. A similar sample is at http://www.mybannertaxi.com/scripts/echo.html

0
Comment
Question by:Brainstormer
  • 10
  • 4
  • 3
  • +2
20 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9679854
maybe this

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function addEmail(fObj){
      fObj.value = fObj.value +"@jay.com";
}      
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<input type="text" name="email" onChange="addEmail(this);">@jAy.com
</form>
</body>
</html>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9679870
wait maybe i missread

Do you want it to do the same thing as the link you posted?
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9679896
You have to remember that it's a PHP file and the form implementation is not done as pure HTML.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9679911
yes i know, can you not just use the script i provided and add the onchange to the text box?


0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9679953
this is the complete login.php file contents:


<?php

/**
 * login.php -- simple login screen
 *
 * Copyright (c) 1999-2003 The SquirrelMail Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * This a simple login screen. Some housekeeping is done to clean
 * cookies and find language.
 *
 * $Id: login.php,v 1.98.2.1 2003/07/03 20:24:47 stekkel Exp $
 */

/* Path for SquirrelMail required files. */
define('SM_PATH','../');

/* SquirrelMail required files. */
require_once(SM_PATH . 'functions/strings.php');
require_once(SM_PATH . 'config/config.php');
require_once(SM_PATH . 'functions/i18n.php');
require_once(SM_PATH . 'functions/plugin.php');
require_once(SM_PATH . 'functions/constants.php');
require_once(SM_PATH . 'functions/page_header.php');
require_once(SM_PATH . 'functions/html.php');
require_once(SM_PATH . 'functions/global.php');

/*
 * $squirrelmail_language is set by a cookie when the user selects
 * language and logs out
 */
set_up_language($squirrelmail_language, TRUE, TRUE);

/**
 * Find out the base URI to set cookies.
 */
if (!function_exists('sqm_baseuri')){
    require_once(SM_PATH . 'functions/display_messages.php');
}
$base_uri = sqm_baseuri();

/*
 * In case the last session was not terminated properly, make sure
 * we get a new one.
 */

sqsession_destroy();
 
header('Pragma: no-cache');

do_hook('login_cookie');

/* Output the javascript onload function. */

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function squirrelmail_loginpage_onload() {\n".
          "    document.forms[0].js_autodetect_results.value = '" . SMPREF_JS_ON . "';\n".
          "    var textElements = 0;\n".
          "    for (i = 0; i < document.forms[0].elements.length; i++) {\n".
          "      if (document.forms[0].elements[i].type == \"text\" || document.forms[0].elements[i].type == \"password\") {\n".
          "        textElements++;\n".
          "        if (textElements == " . (isset($loginname) ? 2 : 1) . ") {\n".
          "          document.forms[0].elements[i].focus();\n".
          "          break;\n".
          "        }\n".
          "      }\n".
          "    }\n".
          "  }\n".
          "// -->\n".
          "</script>\n";
$custom_css = 'none';          
displayHtmlHeader( "$org_name - " . _("Login"), $header, FALSE );

echo '<body text="#000000" bgcolor="#FFFFFF" link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="squirrelmail_loginpage_onload();">' .
     "\n" . '<form action="redirect.php" method="post">' . "\n";

$username_form_name = 'login_username';
$password_form_name = 'secretkey';
do_hook('login_top');

$loginname_value = (sqGetGlobalVar('loginname', $loginname) ? htmlspecialchars($loginname) : '');

/* If they don't have a logo, don't bother.. */
if (isset($org_logo) && $org_logo) {
    /* Display width and height like good little people */
    $width_and_height = '';
    if (isset($org_logo_width) && is_numeric($org_logo_width) &&
     $org_logo_width>0) {
        $width_and_height = " width=\"$org_logo_width\"";
    }
    if (isset($org_logo_height) && is_numeric($org_logo_height) &&
     $org_logo_height>0) {
        $width_and_height .= " height=\"$org_logo_height\"";
    }
}

echo html_tag( 'table',
    html_tag( 'tr',
        html_tag( 'td',
            '<center>'.
            ( isset($org_logo) && $org_logo
              ? '<img src="' . $org_logo . '" alt="' .
                sprintf(_("%s Logo"), $org_name) .'"' . $width_and_height .
                ' /><br />' . "\n"
              : '' ).
            ( (isset($hide_sm_attributions) && $hide_sm_attributions) ? '' :
            '<small>' . sprintf (_("SquirrelMail version %s"), $version) . '<br />' ."\n".
            '  ' . _("By the SquirrelMail Development Team") . '<br /></small>' . "\n" ) .
            html_tag( 'table',
                html_tag( 'tr',
                    html_tag( 'td',
                        '<b>' . sprintf (_("%s Login"), $org_name) . "</b>\n",
                    'center', '#DCDCDC' )
                ) .
                html_tag( 'tr',
                    html_tag( 'td',  "\n" .
                        html_tag( 'table',
                            html_tag( 'tr',
                                html_tag( 'td',
                                    _("Name:") ,
                                'right', '', 'width="30%"' ) .
                                html_tag( 'td',
                                    '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" />' ,
                                'left', '', 'width="*"' )
                                ) . "\n" .
                            html_tag( 'tr',
                                html_tag( 'td',
                                    _("Password:") ,
                                'right', '', 'width="30%"' ) .
                                html_tag( 'td',
                                    '<input type="password" name="' . $password_form_name . '" />' . "\n" .
                                    '<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF" />' . "\n" .
                                    '<input type="hidden" name="just_logged_in" value="1" />' . "\n",
                                'left', '', 'width="*"' )
                            ) ,
                        'center', '#ffffff', 'border="0" width="100%"' ) ,
                    'left', '#FFFFFF' )
                ) .
                html_tag( 'tr',
                    html_tag( 'td',
                        '<center><input type="submit" value="' . _("Login") . '" /></center>',
                    'left' )
                ),
            '', '#ffffff', 'border="0" width="350"' ) . '</center>',
        'center' )
    ) ,
'', '#ffffff', 'border="0" cellspacing="0" cellpadding="0" width="100%"' );
do_hook('login_form');
echo '</form>' . "\n";

do_hook('login_bottom');
echo "</body>\n".
     "</html>\n";
?>
0
 
LVL 3

Expert Comment

by:makc
ID: 9680362
so, is that username that should be appended with domain?

find string:
<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" />

and append
<select name="quickhack"><option>@some.com</option><option>@awesome.net</option></select>

than, find
><input type="submit" value="' . _("Login") . '"

replace "submit" with "button" and add
onclick="document.forms[0].elements.' . $username_form_name . '.value+=document.forms[0].elements.quickhack.options[document.forms[0].elements.quickhack.selectedIndex].text"
0
 
LVL 3

Expert Comment

by:makc
ID: 9680378
...****. forgot. last line in previous post, at the very end: .text" should be .text;document.forms[0].submit()"
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9682029
Sorry makc, I lost you in the code. I tried to patch but did not work. Can you present a more detailed code patch with the new code already in place?

I will try to use jaysolomon's approach. His was correct for HTML, it just needs implementing thru the PHP jungle :-)
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9682211
changes to login.php:


$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addEmail(fObj){ \n".
          "         fObj.value = fObj.value + "@mysite.net"; \n".
          "         } \n".
          "  function squirrelmail_loginpage_onload() {\n".

...

                                    '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .' onChange="addEmail(this);" />' ,
                                'left', '', 'width="*"' )

but I get a : Parse error: parse error, unexpected '@' in ...\wwwroot\webmail\src\login.php on line 58

I used \@ but that did not work. I took out @ completely, but it gave me another error for unexpected T_STRING

any input?
0
 
LVL 1

Expert Comment

by:rstorey2079
ID: 9683246
What you need is the onBlur javascript event handler.  Use it like this:

<?php
if($_POST['submit']=="submit") {
      echo "Username was: $_POST[username]<br>";

}
?>
<html>
<head>
<title>Add Domain Test</title>
<script type="text/javascript" language="Javascript1.2">

function addDomain(element) {

      element.value = element.value + "@hotmail.com";

}

</script>
</head>

<body>


<form action="<?php echo $PHP_SELF; ?>" method="post" name="addDomainTest">
<input type="text" name="username" onBlur="javascript:addDomain(this);"><br>
<input type="text" name="password"><br>
<input type="submit" value="submit" name="submit">
</form>

</body>
</html>
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 6

Author Comment

by:Brainstormer
ID: 9683825
Again, althugh both your answers are correct for simple HTML, I can not implement this in PHP. Either the @ character in @domain.net or the whole @domain.net is causing a PHP parse error.

Parse error: parse error in /home/public_html/mail/src/login.php on line 58

which is:

"  function function addDomain(element) {\n".                                   // added the custom JS    line 57
"        element.value = element.value + "@domain.net";\n".               // in these lines               line 58
"        }\n".                                                                                    // here for domain           line 59

any suggestions with PHP implementation?
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9683838
I mistakenly typed:
function function addDomain(element) {\n"

the actual code for line 57 is:
function addDomain(element) {\n"
0
 
LVL 1

Expert Comment

by:rstorey2079
ID: 9686100
I'm not sure why you are putting the javascript function into the PHP code, but if you are, you do have some parse error:

should be:

"  function function addDomain(element) {\n".                                   // added the custom JS    line 57
"        element.value = element.value + \"@domain.net]\";\n".               // in these lines               line 58
"        }\n".    

notice the way I have escaped the double quotes in line 58 with slashes.  that is why you are getting the parse error.  You need those slashes.
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9686773
the code from rstorey2079 worked. the working changes are as follows:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        element.value = element.value + \"@domain.net\";\n".
          "       }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
...

html_tag( 'td',
      '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" onChange="javascript:addDomain(this);" />' ,
      'left', '', 'width="*"' )


but one problem I noticed is that the script adds a @domain.net everytime I change something in the field. Example:

I type USRE and the script adds @domain.com
I type the password, but I notice that USRE should have been USER

I make the changes, but after I leave the field, now the text looks like:  USER@domain.net@domain.net

One workaround would be to erase everything in the field and type USER at a blank box, but I am afraid the user may fail to realize what has happend, and can not see all the  USER@domain.net@domain.net
due to the visible character limit in the field.

Anyone with a suggestion to kill that part of the script if @domain.net is already inserted, or make the script a one time use only?

Currently the script lines are inserted inside an existing javascript from the SquirrelMail author. Can I move them outside, so 2 JavaScripts exist in the page, but one has a 1 time use or die call.

Thanks for your help so far.
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9686794
addition to the above comments:

I assume some users will not know that the script is active and will attempt to add the domain themselves. This will too cause a USERx@domain.net@domain.net
0
 
LVL 1

Expert Comment

by:rstorey2079
ID: 9687909
Change the javascript function to this:

function addDomain(element) {

    var str = element.value;

     if(str.search(/\@domain\.net/)==-1) {
     element.value = element.value + "@hotmail.com";
     }

}

Make sure you properly escape any quotes or slashes if you are outputting this from PHP.
0
 
LVL 1

Accepted Solution

by:
rstorey2079 earned 25 total points
ID: 9687931
Oops, forgot to mention that you now you need to change the domain in two places:  in the if statement and in the assignment statement.
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 9688945
it worked like a charm! Thanks to everyone that contributed to this. I will award the points to  rstorey2079 for the great help in JavaScript and the final working code, even thought it was not embedded in PHP. I figured that part out.

final code for the new SquirrelMail /scr/login.php is as follows:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        var str = element.value; \n".
          "             if(str.search(/\@domain\.net/)==-1) { \n".
          "        element.value = element.value + \"@domain.net\";\n".
          "        }\n".  
          "  }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
...

html_tag( 'td',
     '<input type="text" name="' . $username_form_name .'" value="' . $loginname_value .'" onChange="javascript:addDomain(this);" />' ,
      'left', '', 'width="*"' )
0
 

Expert Comment

by:erisch
ID: 38312788
I added some code to check if the input string contains '@' and replace everything after '@' with the default domain:

$header = "<script language=\"JavaScript\" type=\"text/javascript\">\n" .
          "<!--\n".
          "  function addDomain(element) {\n".
          "        var str = element.value; \n".
          "        var field = str.split('@',2); \n".
          "             if(str.search(/\@domain\.net/)==-1) { \n".
          "        element.value = field[0] + \"@domain.net\";\n".
          "        }\n".  
          "  }\n".  
          "  function squirrelmail_loginpage_onload() {\n".
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 38316165
Thanks erisch, I have since moved on from SquirrelMail to Google Apps for my mail, but hopefully this is useful to others that still use it.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now