iT邦幫忙

0

php 網頁出現程式碼

php

我想請問一下公司有一個PHP的網頁因為OS要升級至CentOS8
所以我把網頁資料轉到新server但測試網頁時會出現程式碼
我想請問是那裡沒設到,謝謝
舊OS centOS 5
Apache/2.2.8 (CentOS)
新OS centOS 8
Apache2.4
圖1是舊OS的,圖2是新OS的.
https://ithelp.ithome.com.tw/upload/images/20201123/20107072hy8wKm2tLZ.jpg
https://ithelp.ithome.com.tw/upload/images/20201123/20107072tlWOXFZxjK.jpg

看更多先前的討論...收起先前的討論...
通靈亡 iT邦高手 1 級 ‧ 2020-11-23 11:07:55 檢舉
應該是php沒有安裝並設定到Apache上
Server端沒辦法正確執行php的程式碼
cheng1279 iT邦新手 4 級 ‧ 2020-11-23 11:30:45 檢舉
有試過網路上的測試PHP信息有出現內容.
通靈亡 iT邦高手 1 級 ‧ 2020-11-23 11:34:30 檢舉
那麼你的php.ini有沒有開啟short_open_tag
https://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags

如果你有使用<?= ?> 或 <? ?> ,也會無法正確執行php程式碼
因為從你的程式碼,我猜測你可能是使用了<?= ?>

如果確認後有開啟short_open_tag 並重啟Apahce後還是有問題
那麼有可能是你有使用較舊的php函式或語法,導致你目前Apache是比較新的php,無法正確執行舊的php程式碼
cheng1279 iT邦新手 4 級 ‧ 2020-11-23 12:05:42 檢舉
short_open_tag 這個有開
我有google到要在php.conf加一段
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>
不過加完後重啓出現
[root@localhost ~]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.
通靈亡 iT邦高手 1 級 ‧ 2020-11-23 12:36:31 檢舉
php_admin_flag engine off 不要亂加... = =
engine off 是用來禁止伺服器執行特定目錄底下的PHP程式碼...

然後請照指示說的,輸入journalctl -xe查看裡面的錯誤訊息...
你要確定你們的舊PHP是否可以在CentOS8的PHP 7.4版上執行哦,可不會只是更改short_tag而已,資料庫連接也改pdo,這一昇級扯到的事很多
ccutmis iT邦高手 2 級 ‧ 2020-11-23 12:45:12 檢舉
網路搜的參考看看...
https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2

一般這就是無法解讀程式碼的情況。
會無法解讀的情況有如下的原因

1.未安裝可解析php程式碼的對應應用。
2.未設定mini-type對應指向php運行解析的程式。或是model對應錯誤。不過後者理論上是會報錯
3.程式碼非用php副檔名。
4.使用了短應用語法如「」。但php.ini並未啟用短應用。

可以先放一支php程式。上面只有

<?php phpinfo();

先試試是否可以正常跑php。並透出php的info資訊出來。

看更多先前的回應...收起先前的回應...
cheng1279 iT邦新手 4 級 ‧ 2020-11-23 11:45:05 檢舉

可以透出php的info資訊

cheng1279 iT邦新手 4 級 ‧ 2020-11-23 12:09:52 檢舉

程式碼開頭原本是<?改成<?php網頁會變空白

程式碼內容如下
"<?
include("calendar.php");

class MyCalendar extends Calendar
{
function getCalendarLink($month, $year)
{
$s = getenv('SCRIPT_NAME');
// Redisplay the current page, but with some parameters
// to set the new month and year
return "$s?month=$month&year=$year";

}
function getDateLink($day, $month, $year)
{ 	
	$link = "";
    // Only link the first day of every month 
	include("setup.php");
	$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
	$sql="SELECT * FROM `semester`";
	$db=mysql_select_db($mysql_database,$conn);
	mysql_query('SET NAMES utf8');
	$result=mysql_query($sql,$conn);
	while($row=mysql_fetch_row($result))
	{
	$temp1[0]=substr($row[1],0,4);
	$temp2[0]=substr($row[2],0,4);
	if($temp1[0]==$year || $temp2[0]==$year)
	{
	$temp1[1]=substr($row[1],5,2);
	$temp1[2]=substr($row[1],8,2);
	$temp2[1]=substr($row[2],5,2);
	$temp2[2]=substr($row[2],8,2);

	if(mktime(0,0,0,$month,$day,$year)>=mktime(0,0,0,$temp1[1],$temp1[2],$temp1[0]) && mktime(0,0,0,$month,$day,$year)<=mktime(0,0,0,$temp2[1],$temp2[2],$temp2[0]))
	{
		if($month<10)$month="0".$month;
		if($day<10)$day="0".$day;
        $date=$year."-".$month."-".$day;
        $link = "classinfo.php?date=$date";
    }
	}
	
	}
   
        
    return $link;
}

}

$d = getdate(time());

//add by alvin 20110105 for correct date select.
$year = $_GET['year'];
$month = $_GET['month'];
//add by alvin 20110105 for correct date select.

if ($month == "")
{
$month = $d["mon"];
}

if ($year == "")
{
$year = $d["year"];
}

$cal = new MyCalendar;
echo $cal->getYearView($year);

?>

跑出PHPINFO的資訊。代表PHP可以正常運行。
但你將其改成<?PHP就會報錯。(其實空白頁因該就是有錯誤了)
可能是有些函數沒載入或是不支援。
看到你還有用 mysql_connect 系列的函式。
推測你的PHP版本是7版,不支援MYSQL相關函數而造成問題。

可試著降版到5.6試試,並查看你的PHPINFO是否有對應的MYSQL庫的載入。5版的正常會預先載入。
7板的已經不會自動載入了。但會有PDO的應用。

通靈亡 iT邦高手 1 級 ‧ 2020-11-23 13:08:39 檢舉

你把Error打開,就會看到錯誤訊息
PHP版本的機率偏高

cheng1279 iT邦新手 4 級 ‧ 2020-11-24 13:14:52 檢舉

放到另一台主機裡,出現一樣的錯誤
PHP:5.5.12 MySQL:5.6.17

降版使用也得要注意 short_open_tag 是否有打開。
基本上我會建議你將所有的

<?

改成

<?php

然後將有用

<?=

的寫法,改成

<?php echo 

改版後,還是先放一支 phpinfo 做測試。
目前看你新的畫面,估計是php無法運行造成只用text模式運行。

如果真的對主機不懂的架設,找個會架設的人會比較好。
感覺你是卡在主機架設上的問題居多。

0
阿峰
iT邦新手 5 級 ‧ 2020-11-24 10:34:47

PHP5 改到PHP7 真是一個痛苦的經驗
原先使用的 mysql_connect 已經不支援, 所有mysql 的函數都要改成 mysqli

如果不打算改程式碼, 也可以把伺服器的PHP版本降到5.5以下

cheng1279 iT邦新手 4 級 ‧ 2020-11-24 11:23:33 檢舉

還沒做到Mysql的部份,單是月歷跑不出來就快玩死我了.
先前廠商是用1.4版的,網路上找到的3.0版也不能用.
先舊版的錯誤一樣

https://github.com/getk2/k2-next/blob/master/modules/mod_k2_tools/includes/calendar.php

https://ithelp.ithome.com.tw/upload/images/20201124/20107072mi8CLLDaYD.jpg

阿峰
其實,php7是連mysqli也不直接支援喔。
因該說是所有跟mysql有關的函式都不會預先掛載套件了。
全統一用pdo取代了。

不過還是可以手動掛載mysqli套件就是了。記得mysql套件好像是不支援了。沒試過,畢竟我都用pdo了。

0

有可能php fcgi 或 fpm沒有載入,有時您在改的php.ini並不是目前正在跑的php所用的。
如果是舊版php所用到的函式,可能要在CentOS8裝舊版php
如果CentOS8要跑其他版本PHP可以參考看看這篇文章。
https://www.cadch.com/modules/news/article.php?storyid=227

我要發表回答

立即登入回答