Jump to content

PW3 - Passing PW Variable to Outside World


ridgedale
 Share

Recommended Posts

I am trying to pass a variable from ProcessWire to the outside world (for the purposes of page redirection).

I've tried the following without any success:

admin.php

    $session->mntnrvar = $upgradePage->maintainer;

and on the page redirected to:

<?php $mntnrvar = $session->mntnrvar; ?>
This website is currently undergoing maintenance by <?php echo $mntnrvar; ?>.

and:

admin.php

$mntvar = $upgradePage->maintainer;
$GLOBALS['mntnrvar'] = $GLOBALS['mntvar'];

and on the page redirected to:

This website is currently undergoing maintenance by <?php echo $mntnrvar; ?>.

In both cases $mntnrvar returns no data. The field is populated and the correct data is returned when called from within PW, but not when I try to pass the data outside of PW. Can anyone give me an idea where I am going wrong?

Any assistance would be greatly appreciated.

Link to comment
Share on other sites

Thanks for your reply @dragan .

I've tried both the following with the same result of no output:

admin.php:

 

$mntvar = $upgradePage->maintainer;
$_SESSION['mntnrvar'] = $mntvar;

or:

$mntvar = $upgradePage->maintainer;
$_SESSION['mntnrvar'] = $_SESSION['mntvar'];

and on the page redirected to:

This website is currently undergoing maintenance by <?php echo($_SESSION['mntnrvar']); ?>.

I'm not sure what I'm doing wrong. ?

Link to comment
Share on other sites

Thank you again for your reply.

Regarding:

2 hours ago, dragan said:

what is $upgradePage->maintainer; ?

$upgradePage is the PW variable for the path to the page and maintainer is the field containing the data.

Despite double-checking the PHP manual I can still not get the data passed between the pages. Below is the full relevant code:

admin.php:

<?php namespace ProcessWire;

/**
 * Admin template just loads the admin application controller, 
 * and admin is just an application built on top of ProcessWire. 
 *
 * This demonstrates how you can use ProcessWire as a front-end 
 * to another application. 
 *
 * Feel free to hook admin-specific functionality from this file, 
 * but remember to leave the require() statement below at the end.
 * 
 */
 
session_start();

$upgradePage = $pages->get('/site-upgrade/');
if($upgradePage->maintenance && $user->isLoggedin() && $user->name !== $upgradePage->maintainer) {
	$session->logout();
	$mntvar = $upgradePage->maintainer;
    $_SESSION["mntnrvar"] = $_SESSION["mntvar"];
    $session->redirect("/mntadm.php", false);
	exit;
}

require($config->paths->adminTemplates . 'controller.php'); 

and the code for the target mntadm.php file is:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php 
echo "<p>This website is currently undergoing maintenance by " . $_SESSION["mntnrvar"] . ".</p>";
?>
</body>
</html>

Everything works as intended apart from the maintainer data being passed.

Link to comment
Share on other sites

A bit OT, but worth a read:

Quote

Careful with the names

Be careful what you name your variables and functions, classes, files, basically anything and everything. Names should be descriptive of what the variables is or what the function does. This helps you read the code easier and it's more obvious what the code does on the first read without much investigation.

https://dev.to/effingkay/4-practices-for-better-code-4nf3

 

  • Like 2
Link to comment
Share on other sites

Sorry, I don't get what you are trying to do and why. Maybe horst's suggestion would have helped, but I think I'd need a better explanation even with better variable naming.

A shot in the dark: Maybe it's easier to bootstrap PW in your "outside world app" instead of passing variables to there?

Link to comment
Share on other sites

Hi bernhard,

Thank you for your reply.

The maintainer field (located on the site-upgrade page within PW) holds the name of the person carrying out the maintenance. I'm trying to pass that name via (a) variable(s) to a page located outside PW. I don't want anyone other than the person carrying out the site maintenance logging into the site for the duration of the maintenance.

Hopefully that clarifies things.

Link to comment
Share on other sites

For the record I managed to resolve the issue by using the following code:

admin.php:

<?php namespace ProcessWire;

/**
 * Admin template just loads the admin application controller, 
 * and admin is just an application built on top of ProcessWire. 
 *
 * This demonstrates how you can use ProcessWire as a front-end 
 * to another application. 
 *
 * Feel free to hook admin-specific functionality from this file, 
 * but remember to leave the require() statement below at the end.
 * 
 */
 
session_start();

$upgradePage = $pages->get('/site-upgrade/');
if($upgradePage->maintenance && $user->isLoggedin() && $user->name !== $upgradePage->maintainer) {
	$session->logout();
	$mntvar = $upgradePage->maintainer;
	$redirurl = "/mntadm.php?upgadm=" . $mntvar;
    $session->redirect($redirurl, false);
    exit;
}

require($config->paths->adminTemplates . 'controller.php');

with the code for the target mntadm.php file being:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php 
echo "<p>This website is currently undergoing maintenance by " . $_GET['upgadm'] . ".</p>";
?>
</body>
</html>

Thanks again to everyone for their input.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...