2014-11-08 72 views
2

我正在使用Flask和python運行localhost網站。我有一些php文件,當用戶點擊一個按鈕時,我想運行它。問題是Flask沒有將PHP文件識別爲PHP代碼,並且代碼在網頁上顯示爲文本。它顯示了所有回顯語句的文本,但這些語句中的單詞與代碼中的變量相對應,允許用戶登錄和註銷網站。我該怎麼辦?重定向到Flask中的PHP文件

Python代碼:

@app.route('/example.php') 
def phpexample(): 
    return render_template('example.php') 

這表明從echo報表生成的文本html頁面。

的PHP代碼(使用example.php):

<?php 
    require ('steamauth/steamauth.php'); 
?> 
<html> 
<head> 
    <title>Eliminate Phishers! Join Steap now</title> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
     <meta name="description" content="" /> 
     <meta name="keywords" content="" /> 
     <!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]--> 
     <script src="js/jquery.min.js"></script> 
     <script src="js/skel.min.js"></script> 
     <script src="js/init.js"></script> 
     <noscript> 
      <link rel="stylesheet" href="css/skel.css" /> 
      <link rel="stylesheet" href="css/style.css" /> 
      <link rel="stylesheet" href="css/style-wide.css" /> 
     </noscript> 
     <!--[if lte IE 8]><link rel="stylesheet" href="css/ie/v8.css" /><![endif]--> 
</head> 
<body> 

     <!-- Header --> 
      <div id="header"> 
       <span class="logo icon fa-paper-plane-o"></span> 
       <h1>Welcome. This is Steap</h1> 
       <p>A website designed to help eliminate phishers 
       <br /> 
       and hackers on Steam.</p> 
      </div> 

     <!-- Main --> 
      <div id="main"> 

       <header class="major container small"> 
        <h3> 
        <?php 

if(!isset($_SESSION['steamid'])) { 

    echo "welcome guest! <br />\n please login "; 
    steamlogin(); //login button 

} else { 
    include ('steamauth/userInfo.php'); 
    $url = $steamprofile['profileurl']; 
    if ($steamprofile['personastate'] == 0) { 
     $state = '<span style="color:#616161";>(Offline)</span>'; 
     $picture = '<span style="color:#616161";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 

    } else if ($steamprofile['personastate'] == 1) { 
     $state = '<span style="color:#006EFF";>(Online)</span>'; 
     $picture = '<span style="border: 10px dotted #006EFF;"><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } else if ($steamprofile['personastate'] == 2) { 
     $state = '<span style="color:#006EFF";>(Busy)</span>'; 
     $picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } else if ($steamprofile['personastate'] == 3) { 
     $state = '<span style="color:#006EFF";>(Away)</span>'; 
     $picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } else if ($steamprofile['personastate'] == 4) { 
     $state = '<span style="color:#006EFF";>(Snooze)</span>'; 
     $picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } else if ($steamprofile['personastate'] == 5) { 
     $state = '<span style="color:#006EFF";>(Looking to Trade)</span>'; 
     $picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } else if ($steamprofile['personastate'] == 6) { 
     $state = '<span style="color:#006EFF";>(Looking to Play)</span>'; 
     $picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>'; 
    } 


    //Protected content 
    echo "Welcome back" . "</br> </br>" . $picture ."</br>". $steamprofile['personaname'] . "</br>" .$state . "</br>". "Steam ID: ". $steamprofile['steamid'] . "</br>"; 
    echo '<a href="' . $url . '">Steam Profile</a>' . "</br> </br>" . "<form action=\"steamauth/logout.php\" method=\"post\"><input value=\"Logout\" type=\"submit\" /></form>"; // Display their avatar! 

}  
?> 
</h3> 
       </header> 

       <footer class="major container small"> 

        <ul class="actions"> 
         <li><a href="index.html" class="button">Get Phishers</a></li> 
        </ul> 
       </footer> 

      </div> 

     <!-- Footer --> 
      <div id="footer"> 
       <div class="container small"> 

        <header class="major last"> 
         <h2>Questions or comments?</h2> 
        </header> 

        <p>Program not working? Not detecting the phishers properly? <br \> Send us a message. We'll be sure to back to you as soon as possible.</p> 

        <form method="post" action="#"> 
         <div class="row collapse-at-2"> 
          <div class="6u"> 
           <input type="text" name="name" placeholder="Name" /> 
          </div> 
          <div class="6u"> 
           <input type="email" name="email" placeholder="Email" /> 
          </div> 
         </div> 
         <div class="row"> 
          <div class="12u"> 
           <textarea name="message" placeholder="Message" rows="6"></textarea> 
          </div> 
         </div> 
         <div class="row"> 
          <div class="12u"> 
           <ul class="actions"> 
            <li><input type="submit" value="Send Message" /></li> 
           </ul> 
          </div> 
         </div> 
        </form> 

        <ul class="icons"> 
         <li><a href="#" class="icon fa-twitter"><span class="label">Twitter</span></a></li> 
         <li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li> 
         <li><a href="#" class="icon fa-instagram"><span class="label">Instagram</span></a></li> 
         <li><a href="#" class="icon fa-github"><span class="label">Github</span></a></li> 
         <li><a href="#" class="icon fa-dribbble"><span class="label">Dribbble</span></a></li> 
        </ul> 

        <ul class="copyright"> 
         <li>&copy; Steap 2014 All rights reserved.</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li> 
        </ul> 

       </div> 
      </div> 

    </body> 
</html> 
+0

你需要一些可以處理PHP的東西。你使用的是什麼http服務器? – dirn 2014-11-08 23:54:59

+0

我正在使用Flask,並在命令提示符下運行上面的python代碼。然後,我在谷歌瀏覽器中打開http://127.0.0.1:5000/example.php,輸出結果只是一堆文本。 – 2014-11-08 23:59:11

+0

@dirn我明白你在說什麼。我實際上沒有可以處理PHP的東西,因爲Flask不能。你建議我用什麼軟件? – 2014-11-09 00:07:20

回答

-3

你有沒有考慮使用JQuery的Ajax?

下面是一個例子:

您有一個名爲get_name.php女巫文件包含:

<?php echo "Hello, my name is John"; ?> 

使用jQuery AJAX功能我叫get_name.php

 $.ajax({ 
      url : 'get_name.php', 
      success : function(data) { 
       console.log(data); 
      } 
     }); 

中的輸出該控制檯將是:

你好,我的名字是約翰

所以,隨着返回的數據,你可以做任何你想做的事情。

+0

我正在使用JQuery。 – 2014-11-08 23:35:23

+0

我的意思是,JQuery Ajax函數,http://api.jquery.com/jquery。ajax – EdvinasJ 2014-11-08 23:36:43

+0

好的。那究竟是什麼?我想使用python和Flask,並打開我上面的php文件。 PHP和Flask與JQuery有什麼關係。代碼你請張貼某種代碼,因爲我不明白你在說什麼。 – 2014-11-08 23:38:24

1

您可以使用send_from_directory從靜態文件夾發送文件。您可以將php文件添加到靜態文件夾。

然後這條路線和功能添加到

@app.route('/example.php') 
def static_from_root(): 
    return send_from_directory(app.static_folder, request.path[1:]) 

而且不要忘記設置您的靜態文件夾中init.py這樣的:

app = Flask(__name__, static_folder='static', static_url_path='/static') 
從現在

所以,不管你什麼時候去主機/ example.php,您的example.php文件在靜態文件夾將被調用。未呈現。

注:我使用這種方式來提供XML文件。我認爲它也可以用於php文件。

+1

你錯過了')'在回程線:) – 2014-12-04 09:06:45

+0

@OrDuan哦感謝糾正。 – taskiner 2014-12-04 09:20:18

+0

此方法將下載php文件,但不會調用它。 – zhihong 2017-09-14 14:53:34