2

I am new to word press but know all the required PHP skills. I am using a form to create session in one page and then accessing those sessions in another page. I have done this already without word press. What i need is someone tells me how to do this in word press. what files i have to change in word press, which code i should enter in which file ?

The main part is i am using google-map-api to calculate the distance between two locations

here is my code so far:
22.php file :

<?php

 session_start();
$pickup = '';
$pickupadd = '';
$dropoff = '';
$dropoffadd = '';
$km = '';
$_SESSION['pickup'] = $pickup;
$_SESSION['pickupadd'] = $pickupadd;
$_SESSION['dropoff'] = $dropoff;
$_SESSION['dropoffadd'] = $dropoffadd;
$_SESSION['km'] = $km;
?>


<form class="uk-form" action="1111.php" method="get"><label style="color:  
#0095da;"> Pick UP</label>
<input id="pickup" name="pickup" class="controls" style="width: 100%;  
border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; height: 
30px;" type="text" placeholder="Enter a Pick UP location" />
<input id="pickup_address" name="pickupadd" class="controls" style="width:  
100%; border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; 
height: 30px; margin-top: 10px;" type="text" placeholder="House#, Street, 
etc." /><label style="color: #0095da;"> Drop OFF</label>
<input id="dropoff" name="dropoff" class="controls" style="width: 100%; 
border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; height: 
30px;" type="text" placeholder="Enter a Drop OFF location" />
<input id="dropoff_address" name="dropoffadd" class="controls" style="width: 
100%; border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; 
height: 30px; margin-top: 10px;" type="text" placeholder="House#, Street, 
etc." /><label style="color: #0095da;"> Pickup Time</label>
<input id="km" name="km" class="controls" type="hidden" value="" />
<input type="submit" />

</form>

1111.php file :

 <?php

session_start();
$pickup = $_SESSION['pickup'] ;
$pickupadd = $_SESSION['pickupadd'] ;
$dropoff = $_SESSION['dropoff'] ;
$dropoffadd = $_SESSION['dropoffadd'] ;
$km = $_SESSION['km'] ;
if (isset($_GET["pickup"]) || isset($_GET["pickupadd"]) ||    
isset($_GET["dropoff"]) || isset($_GET["dropoffadd"]) || isset($_GET["km"])  
) {
$_SESSION["pickup"] = $_GET["pickup"];
$_SESSION["pickupadd"] = $_GET["pickupadd"];
$_SESSION["dropoff"] = $_GET["dropoff"];
$_SESSION["dropoffadd"] = $_GET["dropoffadd"];
$_SESSION["km"] = $_GET["km"];
}

?>

<body>
<?php 

    echo $_SESSION["pickup"]; 
    echo $_SESSION["pickupadd"]; 
    echo $_SESSION["dropoff"]; 
    echo $_SESSION["dropoffadd"]; 
    echo $_SESSION["km"]; 

?>
</body>
1
  • don't do it. sessions are just cookies which have their info saved on the server instead of the browser, so just use cookies properly and your code will be easier to understand.In addition this is not how you should write code for wordpess, start by writting your form as a plugin or page template and then add the functionality to it, not the other way around Jul 13, 2016 at 9:33

2 Answers 2

2

TomC is right, but to build on that here's what I do. Mainly I use this with a global object which I serialise into the session to save and unserialise from the session to use. Here I've just used an array of your variables. This way you don't need to worry about saving into the session as you go and the session use is easily expanded for other data.

My main use is for shopping baskets and orders, so proper sanitisation, validation and nonces are important, but they are left out here to keep the examples clean. See https://developer.wordpress.org/plugins/security/nonces/ and https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

Assuming this is all going into your theme, you have init code similar to TomC's in functions.php

add_action( 'init', 'setup_session' );

function setup_session() {

    session_start();
    global $map_data;

    if (isset($_SESSION['map_data'])) {
        $map_data = unserialize($_SESSION['map_data']);
    } else {
        $map_data = array();    
    }

    process_get();
    /* chain it after session setup, but could also hook into
       init with a lower priority so that $_GET always writes
       over older session data, or put your own action here to
       give multiple functions access to your sessions
    */
}

add_action( 'shutdown', 'save_session' ); 

function save_session() {

    global $map_data;
    if (isset($map_data)) {
        $_SESSION['map_data'] = serialize($map_data);
    }

}

function process_get() {

    // do modify this to:
    // check nonce
    // sanitise
    // validate

`   global $map_data;

    if ( isset($_GET["pickup"]) || isset($_GET["pickupadd"]) ||  isset($_GET["dropoff"]) || isset($_GET["dropoffadd"]) || isset($_GET["km"])  
 ) {
    $map_data["pickup"] = $_GET["pickup"];
    $map_data["pickupadd"] = $_GET["pickupadd"];
    $map_data["dropoff"] = $_GET["dropoff"];
    $map_data["dropoffadd"] = $_GET["dropoffadd"];
    $map_data["km"] = $_GET["km"];
}
// if any of these $_GET vars is set, replace the whole array


}

You're also right that this could go into your template files as long as you start your session before PHP sends out the headers. In a well written theme (and assuming you aren't running any poorly written plugins) anywhere before your HTML tag will work. Hooking into WP actions is a bit more robust in this regard, but putting the $_GET processing into a template would let you easily keep it to one page instead of running it on all of them.

You can go a step further, though this may be more effort than you need in your case, and use custom session code: https://pippinsplugins.com/storing-session-data-in-wordpress-without-_session/

1

You want to use the init hook to start the session such as:

function start_session() {
    if(!session_id()) {
        session_start();
    }
}

add_action('init', 'start_session', 1);

function end_session() {
    session_destroy ();
}

add_action('wp_logout', 'end_session');
add_action('wp_login', 'end_session');

This would go in the functions.php file.

I suggest you have a look here for more info:

https://codex.wordpress.org/Plugin_API/Hooks

14
  • ok, so how can i access them in some other page ?
    – Devilism
    Jul 13, 2016 at 7:21
  • The same as usual. In your page template file, include the following code: <?php echo $_SESSION["pickup"]; ?>
    – TomC
    Jul 13, 2016 at 7:31
  • I have tried this but nothing shows on the second page
    – Devilism
    Jul 13, 2016 at 7:34
  • don't i have to cal that start_session() is header.php or any other file ?
    – Devilism
    Jul 13, 2016 at 7:36
  • Also, i have seen that WP doesn't support session in php. should i have to enable somewhere ?
    – Devilism
    Jul 13, 2016 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.