3

I am sending signature of pc from python to php and in php i want to use signature on next pages so that's why i am storing it in session variable but i am unable to set session variable don't know why? below is my code any help would be appreciated. Here is my python script.

url = 'http://localhost:9090/project/php/signature.php'

username = getpass.getuser ( )

mac_address = uuid.getnode ( )

#process_id = os.getpid ( )

device_name = socket.gethostname ( )

signature = username + str ( mac_address ) +  device_name
print(signature)

post = { 'sign' : signature }
req = urlencode ( post ).encode ( "utf-8" )
req = requests.post(url, post)
#req.add_header ( "Content-type" , "application/x-www-form-urlencoded" )
print(req.text)

Here is my php script.

<?php
session_start();
if(isset($_POST['sign']))
{
    $sign = $_POST['sign'];
    $_SESSION["sig"] = $_POST['sign'];
    if(!is_dir($sign))
    {
        mkdir($sign);
        echo "folder created";
    }
    else
    {
        echo "folder already exists";
    }
}
else
{
    echo "nothing found";
}

var_dump($_SESSION);
?>

when i run php script it successfully creates folder using signature coming from python but output of var_dump($_SESSION) C:\wamp64\www\project\php\signature.php:22: array (size=0) empty if anyone has any solution of this kindly help. Thank you.

sam
  • 309
  • 2
  • 9
  • 1
    I am waiting for response if anyone has any idea kindly help. – sam Jul 07 '21 at 09:46
  • 1
    You need to assign a cookiejar in order to be able to send the correct session_id back to `PHP` in order to restore the correct session, see [here](https://stackoverflow.com/questions/6878418/putting-a-cookie-in-a-cookiejar) – DarkBee Jul 13 '21 at 07:35
  • 1
    @DarkBee can we handle multiple post request in a same php script coming from python? – sam Jul 13 '21 at 09:19
  • Well of course? I mean making the requests to the `PHP` script is just like mimicking an user (with a browser) on any website? – DarkBee Jul 13 '21 at 09:45
  • @DarkBee as we use isset to check whether the data we sent has arrived or not so when we sent multiple request to same php script so we need to check every coming request using isset ? – sam Jul 13 '21 at 09:48
  • I'd suggest using a framework which can handle this, like [symfony](https://symfony.com/) or (the more lightweight) [slim](https://www.slimframework.com/) – DarkBee Jul 13 '21 at 09:58
  • @DarkBee I am only asking can we share data of two different requests means data we received from first request can be used with data we received from second request? – sam Jul 13 '21 at 10:08
  • Well yes, if you send the (same) session cookie back to the (PHP) server it will repopulate any data you've set in the session in the previous request, which is your initial question – DarkBee Jul 13 '21 at 10:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234810/discussion-between-malik-arsalan-aftab-and-darkbee). – sam Jul 13 '21 at 10:15

0 Answers0