2

I am new to python, I am trying to built a website with python, which is having 2 webpages. 'login.html' 'details.html'

But the user directly entering to details page without login also, how can I stop this?

I am able to do it when I am working with django using login_required, but when I am using cgi-bin script python, for webportal How can I stop this?

Please help me on this.

Here is the sample code:

login.html

<form action = "login1.py" method = "POST">
<label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>
<br>
<br>
    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>
<br>
<br>
<br>
    <button type="submit">Login</button>

details.html

<form action = "copy1.py" method = "POST">
<label for="phone num">Phone num:</label>
 <input type = "text" name = "phone num">  <br />
<br >
<br >
<label for="email">Destination path :</label>
 <input type = "text" name = "email" />
 <br>
<br >
<br >
<div id=submit>
<input type = "submit" value = "Submit" />

login.py:

print("Content-type: text/html")
print("")
import cgi,cgitb
def func():

    form = cgi.FieldStorage()
    cgitb.enable()
    username=form.getvalue('uname')
    password  = form.getvalue('psw')
userlist=[x,g,d]
passlist=[m,ldf]
if username in userlist:
            if password in passlist:
                    redirectURL = "http://localhost/test/details.html"
                    print('<html>')
                    print('  <head>')
                    print('    <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />')
                    print('  </head>')
                    print('</html>')
            else:
                    print('<html>')
                    print('<div id="topcorner"> ')
                    print("please enter valid password")
                    print('</div>')
                    print('</html>')
    else:
            print('<html>')
            print('<div id="topcorner"> ')
            print("please enter valid username")
            print('</div>')
            print('</html>')

problem: without login also I am able to open details.html,user should authenticate first before going to some other site. Please help me on this

4
  • You need to provide us the back-end, not the front-end, moreover which framework are you using, e.g., Flask ? Commented Apr 25, 2018 at 13:35
  • I am not using any framework, I am using cgi-bin python, as it is very small website Commented Apr 25, 2018 at 13:41
  • You should look here : stackoverflow.com/questions/8329919/… Commented Apr 25, 2018 at 13:49
  • Actually my problem is not with the authentication,without authentication also used is about to open details.html page, if the user entering the url of details page, it should redirect to the login page if he did not login Commented Apr 25, 2018 at 15:23

1 Answer 1

0

If your problem is not with the authentication, try to add the following lines into your script,

print('Content-Type: text/html')
print('Location: {}'.format(redirectURL))
print() # HTTP says you have to have a blank line between headers and content
print('<html>')
print('  <head>')
...
Sign up to request clarification or add additional context in comments.

1 Comment

whenever I am entering the localhost/deails.html in the address bar, It is opening. I want the user should redirect to localhost/login.html page, eventhough they entered localhost/deails.html, It is just like @login_required in django,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.