SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_REQUEST/POST/GET["xxx"] question

    I need to edit some code so it can work with register globals, but Im not sure how to get variables such as

    PHP Code:
    $send[$type

    into the $_REQUEST[""] tag

    I tried this, but it wont work
    PHP Code:
    $_REQUEST[$send[$type]]; 
    So I need some expert advice

    Thanks

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_POST['a'// post data (from a form)
    $_GET['a'// get data (page.php?a=1)
    $_COOKIE['a'// from a cookie
    $_SESSION['a'// from a session

    $_REQUEST['a'// would be $_POST['a'] 
    I think on the $_REQUEST, someone please correct me if I'm wrong.. Never use $_REQUEST
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think REQUEST is a combination of POST and GET.

    My problem is when I use this code

    PHP Code:
    $_POST["send[$type]"]; 
    The data is not submitted and nothing happends

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    $_REQUEST is combination of _GET _POST _COOKIE

    _RESQUEST is affect by variables_order.

    "This directive describes the order in which PHP registers GET, POST, Cookie, Environment and Built-in variables (G, P, C, E & S respectively, often referred to as EGPCS or GPC). Registration is done from left to right, newer values override older values."

    variables_order = "EGPCS"

    To the OP, what exactly are you doing? Or wanting to achieve?

    If you are wanting to emulate register_globals on then
    PHP Code:
    // Emulate register_globals on
    if (!ini_get('register_globals')) {
        
    $superglobals = array($_SERVER$_ENV$_FILES$_COOKIE$_POST$_GET);

        if (isset(
    $_SESSION)) {
            
    array_unshift($superglobals$_SESSION);
        }

        foreach (
    $superglobals as $superglobal) {
            
    extract($superglobalEXTR_SKIP);
        }

    To be honest you should really think about not using register_globals it is just bad programming.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Norway
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code is not working
    PHP Code:
    <?
    include("header.php");

    $do_sendaid $_REQUEST["do_sendaid"];
    $dest $_REQUEST["dest"];
    $target_id $_REQUEST["target_id"];

    function 
    sendUnits ($type)
    {
        global 
    $users$uera$enemy$eera$send$cansend;
        
    fixInputNum($send[$type]);
    //    $amount = $send[$type];
        
    $amount $_POST['send[$type]'];;    
        if (
    $amount == 0)
            return;
        elseif (
    $amount 0)
            
    TheEnd("Cannot send a negative amount of $uera[$type]!");
        elseif (
    $amount $users[$type])
            
    TheEnd("You don't have that many $uera[$type]!");
        elseif (
    $amount $cansend[$type])
            
    TheEnd("Cannot send more than 20% of your $uera[$type]!");
        else
        {
            
    $users[$type] -= $amount;
            
    $enemy[$type] += $amount;
        }
    }

    function 
    printRow ($type)
    {
        global 
    $users$uera$cansend$convoy;
    ?>
    <tr><td><?if ($type == cash) echo "Money"; else echo $uera[$type]?></td>
        <td class="aright"><?=commas($users[$type])?></td>
        <td class="aright"><?=commas($cansend[$type])?></td>
        <td class="aright"><input type="text" name="send[<?=$type?>]" size="8" value="<?if ($type == seatrp) echo $convoy; else echo 0;?>"></td></tr>
    <?
    }

    if (
    $users[turnsused] <= $config[protection])
        
    TheEnd("Cannot send aid while under protection!");
    if (
    $users[aidcred] == 0)
        
    TheEnd("Cannot send any more foreign aid!");

    $convoy floor($users[networth] / 10000);

    for (
    $i 0$i 4$i++)
        
    $cansend[$trplst[$i]] = round($users[$trplst[$i]] * .20);
    $cansend[cash] = round($users[cash] * .20);
    $cansend[runes] = round($users[runes] * .20);
    $cansend[food] = round($users[food] * .20);

    if (
    $do_sendaid)
    {
        if (
    $users[turns] < 2)
            
    TheEnd("Not enough turns!");
        if (!
    $dest)
            
    TheEnd("You must specify an empire!");
        if (
    $dest == $users[num])
            
    TheEnd("Cannot send aid to yourself!");
        if (
    $users[seatrp] < $convoy)
            
    TheEnd("You don't have enough $uera[seatrp]!");
        
    fixInputNum($send[seatrp]);
        if (
    $send[seatrp] < $convoy)
            
    TheEnd("You must send at least $convoy $uera[seatrp]!");

        
    $enemy loadUser($dest);
        
    $eera loadEra($enemy[era]);

        if (
    $enemy[num] != $dest)
            
    TheEnd("No such empire!");
        if ((
    $enemy[era] != $users[era]) && ($enemy[gate] <= $time) && ($users[gate] <= $time))
            
    TheEnd("Need to open a time portal first!");
        if (
    $enemy[disabled] >= 2)
            
    TheEnd("Cannot send aid to disabled empires!");
        if (
    $enemy[turnsused] <= $config[protection])
            
    TheEnd("Cannot send aid to someone under protection!");
        if (
    $enemy[vacation] > $config[vacationdelay])
            
    TheEnd("Cannot send aid to someone on vacation!");
        if (
    $enemy[networth] > $users[networth] * 3)
            
    TheEnd("That empire is far too large to require your aid!");

        
    $uclan loadClan($users[clan]);
        if (
    $enemy[clan])
        {
            if ((
    $uclan[war1] == $enemy[clan]) || ($uclan[war2] == $enemy[clan])|| ($uclan[war3] == $enemy[clan]))
                
    TheEnd("Your Generals laugh at the idea of sending aid to your enemies.");
            if ((
    $users[clan] == $enemy[clan]) || ($uclan[ally1] == $enemy[clan]) || ($uclan[ally2] == $enemy[clan]) || ($uclan[ally3] == $enemy[clan]))
                
    $users[aidcred]++;    // unlimited aid to allies
        
    }

        for (
    $i 0$i 4$i++)
            
    sendUnits($trplst[$i]);
        
    sendUnits(cash);
        
    sendUnits(runes);
        
    sendUnits(food);

        
    addNews(102,$users,$enemy,$send[seatrp],$send[armtrp],$send[lndtrp],$send[flytrp],$send[cash],$send[runes],$send[food]);
        
    $users[aidcred]--;
        
    saveUserDataNet($users,"networth armtrp lndtrp flytrp seatrp cash runes food aidcred");
        
    saveUserDataNet($enemy,"networth armtrp lndtrp flytrp seatrp cash runes food");
        
    takeTurns(2,aid);
    ?>
    <?=commas
    ($send[seatrp])?> <?=$uera[seatrp]?> have departed with shipment to <b><?=$enemy[empire]?> (#<?=$enemy[num]?>)</b>
    <hr>
    <?
        $convoy 
    floor($users[networth] / 10000);
        for (
    $i 0$i 4$i++)
            
    $cansend[$trplst[$i]] = round($users[$trplst[$i]] * .20);
        
    $cansend[cash] = round($users[cash] * .20);
        
    $cansend[runes] = round($users[runes] * .20);
        
    $cansend[food] = round($users[food] * .20);
    }
    ?>
    Sending aid requires 2 turns and at least <?=$convoy?> <?=$uera[seatrp]?><br>
    We can send up to <?=$users[aidcred]?> shipment<?=plural($users[aidcred],"s","")?>.<br>
    We can send an additional shipment every hour.<br><br>
    <form method="post" action="<?=$config[main]?>?action=aid">
    <table class="inputtable">
    <tr><td colspan="3" class="aright">Send aid to which empire?</td>
        <td><input type="text" name="dest" value="<?=$target_id?>" size="6"></td></tr>
    <tr><th class="aleft">Unit</th>
        <th class="aright">Owned</th>
        <th class="aright">Can Send</th>
        <th class="aright">Send</th></tr>
    <?
    for ($i 0$i 4$i++)
        
    printRow($trplst[$i]);
    printRow(cash);
    printRow(runes);
    printRow(food);
    ?>
    <tr><td colspan="4" class="acenter"><input type="submit" name="do_sendaid" value="Send Assistance"></td></tr>
    </table>
    </form>
    <?
    TheEnd
    ("");
    ?>
    because register_globals is turned off. I want to fix this by using _POST, _GET, _REQUEST etc as you can see I done on the first lines, but there is one variable left called $send[$type] and I cant make that work with any of the $_xxxx[""] functions.

    I think perhaps its because of its square brackets. I tried earlier with some similar code. I replaced $send[$type] with $send_$type and that worked

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blackhat View Post
    I need to edit some code so it can work with register globals, but Im not sure how to get variables such as
    You're trying to make it work without register globals.

    Instead of having the variables directly registered in the global scope, you must retrieve them from the appropriate source. So instead of this:
    PHP Code:
    $send[$type]; 
    You would do this:
    PHP Code:
    $_POST['send'][$type]; 
    You can also use $_REQUEST in lieu of $_POST, if you want to allow the data to come from either GET or POST, but normally, you should decide which source to use. I seldom use $_REQUEST myself.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •