【问题标题】:Concatenating PHP session in a single quoted string在单引号字符串中连接 PHP 会话
【发布时间】:2014-07-04 21:27:47
【问题描述】:

我必须在文件中创建一个输出文件(带有编译错误)。 我最初的做法是这样的:

$output = system("C:\cygwin\bin\bash.exe --login -c '/cygdrive/c/cygwin/bin/g++.exe /cygdrive/c/Server/www/Codice/chorva.cpp -o /cygdrive/c/Server/www/Codice/chorva.exe >> compile.log 2>&1'");

这段代码有效。

但是当我添加会话时,我无法正确处理。我究竟做错了什么? 非常感谢所有帮助!

$command = "/cygdrive/c/cygwin/bin/g++.exe /cygdrive/c/Server/www/Codice/LOGS/".$_SESSION['username']."/chorva.cpp -o /cygdrive/c/Server/www/Codice/LOGS/".$_SESSION['username']."/chorva.exe >> compile.log 2>&1";    
$output = system('C:\cygwin\bin\bash.exe --login -c'.$command);

编辑:我确实使用了session_start(),并且会话用户名确实有一个值

【问题讨论】:

  • 可能是第二个命令中 -c' 后面的missin空格?
  • 你检查$_SESSION返回的值了吗?
  • 是的@Cristian 它确实有价值
  • 你能告诉我们输出变量的内容吗?
  • @Cristian 它没有任何价值,这实际上是问题所在。但它应该检索编译错误

标签: php string session cygwin system


【解决方案1】:

首先 - 您应该通过在代码顶部放置 session_start(); 来启用 PHP 会话,以便 PHP 可以初始化会话 cookie。

要确保会话已设置且值不为空,请使用 isset($_SESSION['username']) 和/或 !empty($_SESSION['username'])

我建议使用var_dump($_SESSION); 来查看值是否正确设置。

【讨论】:

  • 我在此之前添加了 session_start()。抱歉没提。而且我还 echo -ed 会话,它确实有价值。
  • 我注意到,命令周围没有撇号,因为它在第一个有效的示例中。
  • aaaah... 我只是将双引号更改为单引号(或您所说的撇号)
【解决方案2】:

终于搞定了。我是这样做的:

$un = $_SESSION['username'];
$command = "/cygdrive/c/cygwin/bin/g++.exe /cygdrive/c/Server/www/Codice/LOGS/".$un."/chorva.cpp -o /cygdrive/c/Server/www/Codice/LOGS/".$un."/chorva.exe >> LOGS/".$un."/compile.log 2>&1";
$output = system("C:\cygwin\bin\bash.exe --login -c '".$command);

所以我仍然在那里添加了一个撇号。否则,它不起作用

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多