PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to pass a variable in PHP from a Python file?

 September 14, 2022     exec, php, python     No comments   

Issue

I'm trying to call a Python script in a PHP file and pass a variable value from that script after pressing the submit button. I have it:

index.php

<html>
<body>

<form method="post">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    putenv("HOME=/");
    exec('python test.py'); 
    echo $x;
}
?>

</body>
</html>

test.py

 x = 'ass'

Both files are in the same folder. Why is it not working? I'd like to do this without Flask / Django.


Solution

You can't declare a variable in Python and use it directly in PHP after execution. Try to output your variable in Python:

x = 'test'    
print(x)

Now you can get the result of your script by the return value of the exec function:

$x = exec('python test.py'); 
echo $x;


Answered By - marvas
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing