Webpage redirect to the main page with CGI Python

半世苍凉 提交于 2019-11-27 16:12:59
sdaf

You want to implement this: https://en.wikipedia.org/wiki/Post/Redirect/Get

It's much simpler than it sounds. The CGI script that receives the POST must simply produce the following output:

Status: 303 See other
Location: http://lalala.com/themainpage

You can also send a HTTP header from your processing script:

Location: /

After you have processed your answer, you would send the above header. I would recommend you append a random number query string. e.g. python example (assuming you're using the python CGI module):

#!/usr/bin/env python
import cgitb
import random
import YourFormProcessor

cgitb.enable() # Will catch tracebacks and errors for you. Comment it out if you no-longer need it.

if __name__ == '__main__':
  YourFormProcessor.Process_Form() # This is your logic to process the form.

  redirectURL = "/?r=%s" % random.randint(0,100000000)

  print 'Content-Type: text/html'
  print 'Location: %s' % redirectURL
  print # HTTP says you have to have a blank line between headers and content
  print '<html>'
  print '  <head>'
  print '    <meta http-equiv="refresh" content="0;url=%s" />' % redirectURL
  print '    <title>You are going to be redirected</title>'
  print '  </head>' 
  print '  <body>'
  print '    Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
  print '  </body>'
  print '</html>'
<html> 
  <head> 
    <meta http-equiv="refresh" content="0;url=http://www.example.com" /> 
    <title>You are going to be redirected</title> 
  </head> 
  <body> 
    Redirecting...
  </body> 
</html>

See meta-refresh drawbacks and alternatives here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!