1

ここに画像の説明を入力PHP で if/else ステートメントを使用しようとしています。現在私がやろうとしているのは、ユーザーがアクセスしようとしている現在のディレクトリ ( ) 変数$_SESSION['usr'];と等しい場合です。$dir_auth2次に、私が持っているディレクトリまたは index.php にアクセスできます。それ以外の場合$_SESSION['usr'];!=、現在のディレクトリにある場合は、それらをホームページにリダイレクトします。現在、ユーザーが他のユーザーのディレクトリに入力した場合、そこにはアクセスできません。

<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
//This if statement below is the problem
if($_SESSION['usr'] == $dir_auth1) {
  //This demo.php is the home page
  header("Location: demo.php");

} else {
  echo "You are logged in as " . $dir_auth1;
} 



$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);


$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
echo $_SESSION['usr'];
echo $dir_auth1;

 $dir_user = getcwd();
 $dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);


?>
4

2 に答える 2

1

これがあなたが探しているものだと思います。

$dir_auth1if/else ステートメントで変数を使用する前に、変数を定義する必要があります。

また、あなたが望む!=のは==

<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();

$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
$dir_user = getcwd();
$dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);


if($_SESSION['usr'] != $dir_auth1) {
    header("Location: demo.php");
} else {
    echo "You are logged in as " . $dir_auth1;
} 
?>

また、次のように、すべての文字列関数を 1 つに結合することもできます。

$dir_auth1 = str_replace(array("/home/pophub/public_html/","/home/pophub/public_html/gallry/"),"",getcwd());
于 2016-02-08T21:19:30.017 に答える