Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

html - How to override PHP variables after they have been set?

I have created a System Management page to modify server paths and additional information. The purpose of this page is to modify this values as needed when transferring the pages to a different server. My question is, can I override the values after they have been initialized. I want the values to be override and stored like that from then on. If so, how can I do it? I am currently using input boxes, but the pages keep reading the original value instead of the new value.

Ex. 1st. root path = /var/www/html/wordpress/ (original)

2nd. root path = /var/www/html/sites/pages/ (new value)

This include: site name, root path, administrator email and more.

This variables are used on different pages by including the file. Ex.:

tools.php

<?php
// File containing System Variables
define("LOCAL_PATH_ROOT", $_SERVER["DOCUMENT_ROOT"]);
require LOCAL_PATH_ROOT . '/uas_tools/system_management/centralized_management.php';

<p>Please contact admin at
                <a href="mailto:<?= $admin_email ?>?
to request access to this tool.</p>
?>

index.php (where input boxes are)

<div id="processing"></div>
<form action="centralized_management.php" method="POST">

    <h2>System Management</h2>
    <br>

    <div>
        <h4>Paths</h4>
        <p>Root Path: <input name="root_path" type="text" value="/var/www/html/wordpress/" class="adjust"></p>
    </div>
    <br>

    <div>
        <h4>Configuration</h4>
        <p>Number of threads to be used for mapping generation: <input name="cpu_number" type="number" value="32"
                                                                       class="adjust"></p>
        <p>Administrator Email: <input name="admin_email" type="email" value="[email protected]" class="adjust"></p>
    </div>
    <br>

    <div style="text-align:center ; border:none">
        <input name="submit" type="submit" value="Submit">
    </div>

</form>

centralized_management.php (where values are stored)

<?php
// Paths
$root_path = "/var/www/html/wordpress/";

// Configuration
$cpu_number = "16";
$admin_email = "[email protected]";

if (isset($_POST['submit'])) { // the POST form has been submitted
    // PATHS
    // root_path
    if (!empty($_POST['root_path'])) {
        $root_path = ($_POST['root_path']);
    }

    // cpu_number
    if (!empty($_POST['cpu_number'])) {
        $cpu_number = ($_POST['cpu_number']);
    }

    // admin_email
    if (!empty($_POST['admin_email'])) {
        $admin_email = ($_POST['admin_email']);
    }
}
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As suggested by @Definitely not Rafal, I created a table called 'system_management' with 3 columns (ID, Name, Value).

Here I added the variables needed and their respective value.

tools.php stays the same.

index.php (where input boxes are)

<?php
$conn = SetDBConnection();

            $sql = "SELECT Name, Value FROM system_management WHERE ID = 1";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $root_path = $row["Value"];
                }
            }
mysqli_close($conn);
            ?>

<div id="processing"></div>
<form action="centralized_management.php" method="POST">

    <h2>System Management</h2>
    <br>

    <div>
        <h4>Paths</h4>
        <p>Root Path: <input name="root_path" type="text" value="<?= $root_path ?>" class="adjust"></p>
    </div>
    <br>
</form>

centralized_management.php (where values are stored)

<?
$conn = SetDBConnection();

if (isset($_POST['submit'])) { // the POST form has been submitted
    // root_path
    if (!empty($_POST['root_path'])) {
        $root_path = ($_POST['root_path']);

        $sql = "UPDATE system_management set Value = '$root_path' where Name = 'root_path'";
        $result = mysqli_query($conn, $sql);
    }
    }

$sql = "SELECT Name, Value FROM system_management WHERE ID = 1";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $root_path = $row["Value"];
    }
}

mysqli_close($conn);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...