注销后为什么需要刷新页面?

时间:2018-03-13 16:08:38

标签: php

这是我的HTML和PHP:

<a href="index.php?logout=true">logout</a>
<?php
if (isset($_GET['logout'])) {
    $_SESSION = array();
    session_unset();
    header("location:index.php");
}
?>

这是我的JavaScript代码,用于显示是否已登录信息:

// i call this load function on <body onload="load()">
function load() {
    if (document.getElementById('username').innerHTML === "") {
        document.getElementById('login').style.display = "inline";
        document.getElementById('signup').style.display = "inline";
        document.getElementById('udahlogin').style.display = "none";
    } else {
        document.getElementById('udahlogin').style.display = "inline";
        document.getElementById('login').style.display = "none";
        document.getElementById('signup').style.display = "none";
    }
}

这是我在标题中的代码,它会显示信息,如果它是基于我的javascript代码登录的话:

<li>
    <a id="udahlogin" onclick="document.getElementById('id03').style.display='block'"> Selamat datang, <span id="username"><?php echo $_SESSION['username']?></span></a>
</li>
<li id="login">
    <a class="loup" id="login" onclick="document.getElementById('id01').style.display='block'"> &#xf08b; Login</a>
</li>
<li id="login">
    <a class="loup" id="signup" onclick="document.getElementById('id02').style.display='block'">Sign Up</a>
</li>

我已经尝试在该元素上使用onclick事件并重新加载,例如:

<a href="index.php" onclick="logout()">logout</a>

和JavaScript:

function logout(){
    window.location.reload();
    //or
    window.location.reload(true);
}

但它仍然需要刷新,而且在php中我也已经在index.php之前使用了这段代码:

header("Refresh:0");

但它没有显示任何内容。

3 个答案:

答案 0 :(得分:0)

在页面加载之前确定页面的会话。你应该:

  1. 在再次调用会话之前将用户注销
    1. 将用户注销,然后刷新或重定向
    2. 例如,对于选项2,您可以创建将用户注销的 @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String languageToLoad = "en"; // your language Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getBaseContext().getResources().getDisplayMetrics()); final View view = inflater.inflate(R.layout.ijad_forooshgah, container, false); int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA); //---------------------------------- if(permissionCheck == PackageManager.PERMISSION_DENIED) RequestRuntimePermission(); return view; } private void GalleryOpen() { GalIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(Intent.createChooser(GalIntent,"Select Image from Gallery"),2); } private void CameraOpen() { CamIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(Environment.getExternalStorageDirectory(), "file"+String.valueOf(System.currentTimeMillis())+".jpg"); uri = Uri.fromFile(file); CamIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri); CamIntent.putExtra("return-data",true); startActivityForResult(CamIntent,0); } private void CropImage() { try{ CropIntent = new Intent("com.android.camera.action.CROP"); CropIntent.setDataAndType(uri,"image/*"); CropIntent.putExtra("crop","true"); CropIntent.putExtra("outputX",180); CropIntent.putExtra("outputY",180); CropIntent.putExtra("aspectX",4); CropIntent.putExtra("aspectY",4); CropIntent.putExtra("scaleUpIfNeeded",true); CropIntent.putExtra("return-data",true); startActivityForResult(CropIntent,1); } catch (ActivityNotFoundException ex) { } }`private void RequestRuntimePermission() { if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.CAMERA)) Toast.makeText(getActivity(),"CAMERA permission allows us to access CAMERA app",Toast.LENGTH_SHORT).show(); else { ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.CAMERA},RequestPermissionCode); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case RequestPermissionCode: { if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) Toast.makeText(getActivity(),"Permission Granted",Toast.LENGTH_SHORT).show(); else Toast.makeText(getActivity(),"Permission Canceled",Toast.LENGTH_SHORT).show(); } break; } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.imageToUpload: AlertDialog.Builder ab = new AlertDialog.Builder(getActivity()); ab.setMessage("انتخاب عکس"); ab.setPositiveButton("دوربین", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { CameraOpen(); } }); ab.setNegativeButton("گالری", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { GalleryOpen(); } }); ab.show(); break; ,然后将其重定向到logout.php

答案 1 :(得分:0)

一旦在文档上打印了某些内容,就不应该通过标题重定向。因此,如果您将其编码为此,则应该给出错误。

<a href="index.php?logout=true">logout</a>
<?php
    if (isset($_GET['logout'])) {
        $_SESSION = array();
        session_unset();
        header("location:index.php");
    }
 ?>

尝试在html标记

之前将该PHP部分移动到文档的顶部

答案 2 :(得分:0)

这就是我使用的。它很简单,您只需将其放在现有PHP代码的末尾:

<class 'str'> ['email@email.com ']