0

the other day i saw in stack overflow how to get redirected using a function.. but its not working here

#!/Python27/python
import cgi
import cgitb ; cgitb.enable()
import MySQLdb
from datetime import datetime
f= cgi.FieldStorage()
l=f["roll"].value
def redirect(url):
    print "Content-Type: text/plain"
    print "Refresh: 0; url='%s'" % url
    print
    print "Redirecting..."

Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="jesus",        db="student")
cursor = Con.cursor()
sql="SELECT YEAR FROM STDDET WHERE id='%s'"%(l)
cursor.execute(sql)
data=cursor.fetchone()
cyear=datetime.now().year
cmonth=datetime.now().month
data=data[0]
data=int(data)
year=cyear-data
print year
if(year==3):
    redirect("intro.html")
elif(year==4):
     redirect("yea4.html")

even though the if condition is satisfied its not getting redirected...am getting an error malformed header...plz help!!

user2216762
  • 11
  • 1
  • 5

1 Answers1

0

As far as I can tell, your script produce the following response:

Content-Type: text/plain Refresh: 0; url='x.y.com'

Redirecting...

This is not a valid http response since the status line is missing.

If you produce a "valid" page, you should reply with a status

HTTP/1.1 302 Found

If you are redirecting, a better idea would be to reply with one of the 3xx code. As an example:

HTTP/1.1 302 Found
Location: http://www.google.fr/

The HTTP/1.1 ... line must be the first of the header. So it should be produced by the first print of your program. See Python CGI returning an http status code, such as 403? for an example.

Community
  • 1
  • 1
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • can i use http header response to redirect based on a condition...i edited the codee.....can you please tell me whether http header will work for that code?? if not plz help me with an answer – user2216762 Jun 17 '13 at 02:34