简体   繁体   中英

How to redirect onto other page with a passing function call along with it?

//here after session created i must redirect to 'home.php' along with a function call on the page 'home.php'

<?php
if($postname == $storedUsername && $postPassword == $storedPassword)
                { 
                    $_SESSION['authentication'] = 1;
                    header('location:home.php');  

?>  

There are a few different ways to do this.

Method 1

The simplest way would be using a GET parameter:

<?php
if($postname == $storedUsername && $postPassword == $storedPassword)
                { 
                    $_SESSION['authentication'] = 1;
                    header('Location: home.php?loadfunc=true');  
                    die();
                 }

?>  

Note: You did not close the brace above in your original code...not sure if this was intentional. You also did not invoke a die or exit function after your header redirection. This is dangerous to do as people will still be able to access the rest of your code if you do not want them to.

Then in the home.php page do the following:

if (!empty($_GET['loadfunc'])){
    //call function here
}

Note: Anyone can type a GET parameter into the address bar and anyone can read the address using a packet sniffer. If you do not want people to be able to do this, do not use this method.

Method 2

A better way to do it might be to just check the SESSION variable on the home.php page:

session_start();
.... 
if (!empty($_SESSION['authentication']) && $_SESSION['authentication'] == 1){
     //call function here
}

I recommend this last method if it works for your code. I

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM