pythonpython-3.xweb-scrapingpython-requestsphpbb

login to phpBB programmatically


I am trying to log in to a phpBB forum. However, I cannot figure out what is wrong with the code.

import requests

forum = "https://adblockplus.org/forum/"

headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username': 'username', 'password': 'password'}
session = requests.Session()

r = session.post(forum + "ucp.php?mode=login", headers=headers, data=payload)
sidStart = r.text.find("sid")+4
sid = r.text[sidStart:sidStart+32]
parameters = {'mode': 'login', 'sid': sid}
r = session.post(forum, params=parameters)

if "Logout" in r.text:
    print("We are in")
else:
    print(r.text)

print(r)

It just always ends up not logged in.


Solution

  • import requests
    forum = "https://adblockplus.org/forum/"
    
    headers = {'User-Agent': 'Mozilla/5.0'}
    payload = {'username': 'username', 'password': 'password', 'redirect':'index.php', 'sid':'', 'login':'Login'}
    session = requests.Session()
    
    r = session.post(forum + "ucp.php?mode=login", headers=headers, data=payload)
    print(r.text)
    

    Made some small changes like adding redirect, sid and login to payload and it seems to work. Not sure which one helped, I'll leave figuring that out up to you.