0

私がやろうとしているのは、クエリが空であるか、コード名が間違っている場合に、訪問者をエラー ページに送ることです。私の現在のスクリプトは、次の「このウェブサイトにはハッキングはありません!」をエコーし​​ますが、これはすべて、元のクエリが送信された同じページで発生しています。

目標は、最初のページと同じフォルダー内にあるカスタム エラー ページに訪問者を送ることです。

http://www.example.com/download/file.php 

http://www.example.com/download/error.php 

そして私の現在のスクリプトは

    <?php
$host = 'localhost';
$db_name = 'My_DB_Name';
$db_user = 'MY_User_Name';
$db_pass = '******';
$path_to_file_directory = 'download/files/';
mysql_connect( $host, $db_user, $db_pass);
mysql_select_db( $db_name ) or die(mysql_error());

$file_code = filter_input( INPUT_GET, 'urlid' );

# Query for file info
$res = mysql_query("SELECT * FROM `Files` WHERE `urlID`='".$file_code."'") or die ( mysql_error() );

# If query is empty, there is a bad code name
# This catches possible hacking attempt.
if( mysql_num_rows($res) == 0 )
{
echo 'There will be no hacking on this website! ';
exit();
} 

# Save file info into an array called "$info"
$info = mysql_fetch_assoc($res); 

# File path is below
$file_path = $path_to_file_directory.$info['file_name'];

# Now push the download through the browser
# There is more than 1 way to do this.
if (file_exists($file_path)) {
echo '<p>if it does not: <a href="'.$file_path.'">click here to try again</a>.</p>';
exit;
}

?> 

エコーを別のものに変更する必要があることは理解していますが、エラーページへのリンクをエコー内に配置するだけでは、file.phpページでのみエコーされるため、代わりに何を使用する必要があるかわかりません

助けてください

4

2 に答える 2

2

必要なのは、別のページへのリダイレクトだと思います。headerそのための機能を使用できます。

<?php
if( mysql_num_rows($res) == 0 )
{
  header('Location: /nohacking.php');
  exit();
} 
?>

これにより、ブラウザが nohacking.php ファイルにリダイレクトされます。

于 2013-03-05T18:00:49.970 に答える
1

まだヘッダーを送信していない限り (つまり、基本的に、これは HTML の前です):

if(mysql_num_rows($res) == 0)
{
die(header("Location: error.php"));
}

header() 関数はページをリダイレクトし、die() 関数は残りの PHP の処理を​​停止します。

于 2013-03-05T18:00:37.667 に答える