log_in.py

<---Back

Select Code Highlighting Style:

BRIGHT | Seashell | Darkness

Select Font Size:

Small | Normal | Large
../www/get_script_html_files/strip/log_in.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This is function to check email/password of user and
    6 # set cookie if user logged in successfully
    7 #
    8 # Version: 1.14
    9 #
   10 #######################################
   11 
   12 
   13 #import all functions from lib.py
   14 
   15 from lib import *
   16 
   17 #this flag will be set to 1 unless email and password provided by user match those in DB
   18 
   19 is_correct_user = 1
   20 
   21 #try/except pair is a mechanizm to handle exceptions
   22 #in a nutshell, if anything under try: goes wrong, program
   23 #doesn't exit with error, instead execution shifts to statements
   24 #under except:
   25 
   26 
   27 try:
   28 
   29     #get form data
   30 
   31     form = cgi.FieldStorage()
   32 
   33     email = form['email'].value
   34 
   35     password_user = form['password'].value
   36 
   37     #check if password is correct
   38 
   39     sql_line = "select password,id from users where email='%s'" % email.lower()
   40 
   41     tmp_list = select_one(sql_line)
   42 
   43     password_db =   tmp_list[0]
   44     user_id     =   tmp_list[1]
   45 
   46     #if password provided by user matches password from DB
   47 
   48     if password_user == password_db:
   49 
   50 
   51         #Set cookies on hard drive of user
   52         #You can go to  Test Portal to see cookies set by ShareLane
   53 
   54         cookie = Cookie.SimpleCookie()
   55 
   56         cookie['user_id'] = user_id
   57         cookie['email'] = email
   58 
   59         #print time_expire. cookie will expire in 1 hour
   60 
   61         cookie['user_id']['expires'] = 3600
   62         cookie['email']['expires'] = 3600
   63 
   64         print cookie
   65 
   66         is_correct_user = 0
   67 
   68 except:
   69 
   70     pass
   71 
   72 
   73 ############if user provided correct email/password we
   74 #   1. show nice page with progress indicator
   75 #   2. redirect user to homepage
   76 
   77 print 'Content-Type: text/html\n\n'
   78 
   79 if is_correct_user == 0:
   80 
   81 
   82     html="""<html>
   83 
   84         <title>Redirecting...</title>
   85 
   86         <head>
   87             <link rel="stylesheet" type="text/css" href="http://www.sharelane.com/styles.css" />
   88 
   89         </head>
   90 
   91         <body>
   92 
   93             <table align="center" border=0 valign="middle">
   94 
   95                 <tr align="center">
   96 
   97                     <td><img src="../images/logo.jpg" border=0></td>
   98 
   99                 </tr>
  100 
  101                 <tr align="center">
  102 
  103                     <td><img src="../images/pixel.gif" height=49  border=0></td>
  104 
  105                 </tr>
  106 
  107                 <tr align="center">
  108 
  109                     <td><p class="title_10">Redirecting...</td>
  110 
  111                 <tr>"""
  112 
  113     #below is the reference to JavaScript file timer.js
  114     #timer.js show progress indicator and redirects browser to the file http://www.sharelane/cgi-bin/main.py
  115     #timer.js was written by Brian Gosselin of http://scriptasylum.com.
  116 
  117     html=html+"""
  118 
  119                     <td><p align="left">
  120                     <!--webbot bot="HTMLMarkup" startspan -->
  121                     <script language="javascript" src="../timer.js">
  122                     </script>
  123                     <!--webbot bot="HTMLMarkup" endspan i-checksum="37726" --></td>
  124 
  125                 </tr>
  126             </table>
  127 
  128         </body>
  129         </html>"""
  130 
  131 
  132     print html
  133 
  134 else:
  135 
  136 
  137     message = "Oops, error. Email and/or password don't match our records"
  138 
  139     is_logged_in = 1
  140     body_html = ''
  141     caption = ''
  142 
  143     html=get_html(is_logged_in,body_html,caption,message)
  144 
  145     print html
  146