Toon posts:

[PHP] Apache mod_rewrite maakt van POST een GET

Pagina: 1
Acties:

Verwijderd

Topicstarter
Dit toci kan naar mijn mening ook prima in Software algemeen, hoort hij vast beter thuis... mij is altijd geleerd... bij twijfel niet doen... dus dat kan zijn hem daar plaatsen of hem hier posten. De keus laat ik even over aan onze aardige mods :+

Ik heb een probleem met een form wat ik post en welke nooit "aankwam".

Het blijkt dat doordat ik een simpele mod_rewrite rule gebruik deze rule mijn POST omzet naar een GET.

Opzich vind ik het vrij logisch dat het gebeurt, echter kan ik het niet oplossen door simpelweg te zoeken, dit vergt een discussie :)

Mijn rule is:

code:
1
2
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]


Waarbij ik post naar een /pagina waar pagina eigenlijk pagina.php is maar wat ik herschrijf doormiddel vane ens tukje simpele PHP code dat de include invult door "pagina" in een string met .php zet zetten waardoor toch pagina.php geinclude wordt.

Daar kan het opzich ook mis gaan aangezien ik natuurlijk een semi-rewrite rule gebruik maar welke PHP verzorgt door dat stukje code.

Ik post overigens mijn form wel naar /pagina war verder goed werkt want zodra ik een if() op GET zet inplaats van POST krijg ik de data netjes door de juiste if() alleen uiteraard met een bunch ongedefinieerde variabelen omdat get een GET is geworden ;)

Is dit een issue tussen mijn phpcode voor de .php en de mod_rewrite rule ?

Die php code had ik gebruikt en later de mod_rewrite rule toegepast... dus de fout is opvolgend redeijk logisch te noemen op dat front ;) :+ ( exit(praat me eruit modus); )

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Verwijderd schreef op donderdag 27 november 2008 @ 01:45:
Dit toci kan naar mijn mening ook prima in Software algemeen, hoort hij vast beter thuis... mij is altijd geleerd... bij twijfel niet doen... dus dat kan zijn hem daar plaatsen of hem hier posten. De keus laat ik even over aan onze aardige mods :+
Waar hoort mijn topic?
PRG >> WSS

Ik kan verder trouwens geen touw vast knopen aan je post. Zal wel aan mij liggen...

[ Voor 20% gewijzigd door RobIII op 27-11-2008 02:02 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Verwijderd

Topicstarter
RobIII schreef op donderdag 27 november 2008 @ 01:59:
[...]

Ik kan verder trouwens geen touw vast knopen aan je post. Zal wel aan mij liggen...
De eerste post hier legt het ook goed uit:

http://www.webmasterworld.com/apache/3001557.htm

Ik heb een 2- tal van voorbeelden gevonden, maar geen echte oplossing voor mijn versie:

http://www.phphulp.nl/for...at=1&id=33859&lasttopic=1

Verwijderd

Topicstarter
Ik ben er achter dat ook de POST bij het script opzichzelf, dus niet in een CMS of wat dan ook een GET geeft met " echo $_SERVER['REQUEST_METHOD'];"

Opzich is een pagina zelf altijd een GET zonder vanaf een form gekomen te zijn, echter lijkt me dat als er een form gepost is dit toch wel gezien mag worden in de "$_SERVER['REQUEST_METHOD'];"

  • Noork
  • Registratie: Juni 2001
  • Niet online
Post en get kan volgens mij gewoon door elkaar. Jouw rewrite zou er niet voor moeten zorgen dat de post-data opeens wordt omgezet naar een querystring. Wat gebeurd er zonder de rewrite, is je code wel goed?

Post eens relevante stukjes. Inhoud van je form, afvangen van post/get gegevens.

p.s. Kun je ook wat duidelijker zijn/schrijven? Ik moet echt 3x lezen, en nog snap ik het niet.

[ Voor 61% gewijzigd door Noork op 27-11-2008 22:19 ]


Verwijderd

Topicstarter
Ik vind het altijd lastig om hele lappen code neer te zetten, ik wil het graag oplossen zonder het idee te creeren van " ik kwak het neer en lossen jullie het maar op".

Om toch meer duidelijkheid te geven post ik even het bestand waar het om gaat:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php 

    

include_once "config/settings.php";
include_once "class/cart.inc";
include_once "class/customer.inc";
include_once "class/invoice.inc";
include_once "class/states.inc";
include_once "class/taxManager.inc";
include_once "class/shippingManager.inc";

session_start();

// If no cart exists we can't check out
if (!session_is_registered('cart')) {
    // Redirect to root of site
    header("Location: $settings->siteUrl");
    exit;
}   

$cart = $_SESSION['cart'];

// If no session exists, create one
if (!session_is_registered('invoice')) {
    $_SESSION['invoice'] = new invoice( new taxManager($settings->taxRates), 
                                        new shippingManager($settings->shippingRates));
} 

$invoice = $_SESSION['invoice'];
$invoice->cart = $cart;
$displayInvoice = false;
$noValidation=true;
$errorMessage="";
    
// Figure out how we got here. Only post are supported to alter this
// A GET request will simply display the content
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {

    $invoice->customer->billingAddress->name = $_POST['name'];      
    $invoice->customer->billingAddress->street1 = $_POST['street1'];
    $invoice->customer->billingAddress->street2 = $_POST['street2'];
    $invoice->customer->billingAddress->city = $_POST['city'];
    $invoice->customer->billingAddress->zip = $_POST['zip'];
    $invoice->customer->billingAddress->state = $_POST['state'];
    $invoice->customer->email = $_POST['email'];
    $invoice->customer->phone = $_POST['phone'];    
        
    $invoice->shipToBillingAddress = (strlen($_POST['shipToBillingAddress']) > 0);
        
    if ($invoice->shipToBillingAddress) {
        $invoice->shippingAddress->name = $_POST['name'];
        $invoice->shippingAddress->street1 = $_POST['street1'];
        $invoice->shippingAddress->street2 = $_POST['street2'];
        $invoice->shippingAddress->city = $_POST['city'];
        $invoice->shippingAddress->zip = $_POST['zip'];
        $invoice->shippingAddress->state = $_POST['state'];         
    } else {
        $invoice->shippingAddress->name = $_POST['shipToName'];
        $invoice->shippingAddress->street1 = $_POST['shipTostreet1'];
        $invoice->shippingAddress->street2 = $_POST['shipTostreet2'];
        $invoice->shippingAddress->city = $_POST['shipTocity'];
        $invoice->shippingAddress->zip = $_POST['shipTozip'];
        $invoice->shippingAddress->state = $_POST['shipTostate'];           
    }
        
    if ($invoice->validate()) {
        $displayInvoice=true;
    } else {
        $errorMessage="<p class='warning'>Please correct the errors in the <span class='validation-error'>highlighted fields</span> below.</p>";
        $noValidation=false;
    }
}

// This is needed because the variable is a copy of the object, not a reference to it
$_SESSION['invoice'] = $invoice;    

$title = "Invoice";
include "header.php"; 

?>
<div id="invoice">

<? if ($displayInvoice) { ?>
    <div id="navmenu">
        <ul>
            <li class="first">
                <a href="invoice.php">&laquo; Back</a>
            </li>
            <li class="last">
                <a href="#" onclick="document.pay.submit();">Proceed to Secure Payment &raquo;</a>
            </li>
        </ul>
    </div>
    <p class="info">Please verify your information before <a href="#" onclick="document.pay.submit();">proceeding to the secure payment page</a>.</p>
    <div id="billing">
        <fieldset>
            <legend>Billing Information (<a href="invoice.php">edit</a>)</legend>
            <div>
                <label>Order #</label>
                <?= $invoice->orderNumber ?>
            </div>
            <div>
                <label>Name</label>
                <?=$invoice->customer->billingAddress->name?>
            </div>
            <div>
                <label>Address</label>
                <?= $invoice->customer->billingAddress->street1 ?>
            </div>
    <? if (strlen($invoice->customer->billingAddress->street2) > 0) { ?>
            <div>
                <label>&nbsp;</label>
                <?= $invoice->customer->billingAddress->street2 ?>
            </div>
    <? } ?>
            <div>
                <label>City</label>
                <?=$invoice->customer->billingAddress->city?>
            </div>
            <div>
                <label>ZIP</label>
                <?=$invoice->customer->billingAddress->zip?>
            </div>
            <div>
                <label>State</label>
                <?=$invoice->customer->billingAddress->state?>
            </div>
            <div>
                <label>Email</label>
                <?=$invoice->customer->email?>
            </div>
            <div>
                <label>Phone</label>
                <?=$invoice->customer->phone?>
            </div>
        </fieldset>
    </div>
    <div id="shipping">
        <fieldset>
            <legend>Shipping Information (<a href="invoice.php">edit</a>)</legend>
            <div>
                <label>Name</label>
                <?=$invoice->shippingAddress->name?>
            </div>
            <div>
                <label>Address</label>
                <?=$invoice->shippingAddress->street1?>
            </div>
    <? if (strlen($invoice->shippingAddress->street2) > 0) { ?>
            <div>
                <label>&nbsp;</label>
                <?=$invoice->shippingAddress->street2?>
            </div>
    <? } ?>
            <div>
                <label>City</label>
                <?=$invoice->shippingAddress->city?>
            </div>
            <div>
                <label>ZIP</label>
                <?=$invoice->shippingAddress->zip?>
            </div>
            <div>
                <label>State</label>
                <?=$invoice->shippingAddress->state?>
            </div>
        </fieldset>
    </div>
    <hr />
    <div id="items">
        <fieldset>
            <legend>Items (<a href="cart.php">edit</a>)</legend>
            <table>
                <tr>
                    <th>Sku</th>
                    <th>Product</th>
                    <th>Qty</th>
                    <th>Price</th>
                    <th>Total</th>
                </tr>
    
<? 
    $alternate = false;
    foreach ($invoice->cart->items as $id => $item) { 
        $alternate = !$alternate;
?>
                <tr class="<?= $alternate ? "a" : "b" ?>">
                    <td><?=$id?></td>
                    <td><?=$item->name?></td>
                    <td align="center"><?=$item->qty?></td>
                    <td class="currency">$<?=number_format($item->price, 2)?></td>
                    <td class="currency">$<?=number_format($item->extendedPrice(), 2)?></td>
                </tr>
<?  } ?>
                <tr class="b">
                    <td colspan="4" align="right"><strong>Tax (<?=number_format($invoice->getTaxRate()*100, 2)?> %)</strong></td>
                    <td class="currency">$<?=number_format($invoice->getTaxAmount(), 2)?></td>
                </tr>
                <tr class="b">
                    <td colspan="4" align="right"><strong><?=$invoice->getShippingMethod()?> Shipping (<?=number_format($invoice->cart->getTotalWeight(), 2)?> lb)</strong></td>
                    <td class="currency">$<?=number_format($invoice->getShippingAmount(), 2)?></td>
                </tr>
                <tr class="b">
                    <td colspan="4" align="right"><strong>TOTAL</strong></td>
                    <td class="currency">$<?=number_format($invoice->getTotal(), 2)?></td>
                </tr>
            </table>
        </fieldset>
    </div>
<?  
    include "config/paymentProcessor.php";

    } else { 
?>
    <script language="JavaScript" type="text/javascript"><!--
function toggle() {
    document.invoice.shipToName.readOnly = document.invoice.shipToBillingAddress.checked;
    document.invoice.shipTostreet1.readOnly = document.invoice.shipToBillingAddress.checked;
    document.invoice.shipTostreet2.readOnly = document.invoice.shipToBillingAddress.checked;
    document.invoice.shipTozip.readOnly = document.invoice.shipToBillingAddress.checked;
    document.invoice.shipTocity.readOnly = document.invoice.shipToBillingAddress.checked;
    document.invoice.shipTostate.readOnly = document.invoice.shipToBillingAddress.checked;          
}
//-->
    </script>
    <div id="navmenu">
        <ul>
            <li class="first">
                <a href="cart.php">View Cart (<?
        if ($cart->getTotalItems() >= 2) {
            echo $cart->getTotalItems() . " items";
        } else {
            echo $cart->getTotalItems() . " item";
        }
?>)
                </a>
            </li>
            <li     class="last">
                <a href="#" onclick="document.invoice.submit();">Next &raquo;</a>
            </li>
        </ul>
    </div>

    <?= $errorMessage ?>
    <form name="invoice" action="<?=$PHP_SELF?>" method="post" enctype="application/x-www-form-urlencoded">
        <div id="billing">
            <fieldset>
                <legend>Billing Information</legend>
                <div>
                    <label for="name" class="req">Name</label>
                    <input type="text" name="name" id="name" tabindex="1" value="<?=$invoice->customer->billingAddress->name?>" class="<?=$noValidation || $invoice->customer->billingAddress->isNameValid() ? '' : 'validation-error' ?>" />
                </div>
                <div>
                    <label for="street1" class="req">Address</label>
                    <input type="text" name="street1" id="street1" tabindex="2" value="<?=$invoice->customer->billingAddress->street1?>" class="<?=$noValidation || $invoice->customer->billingAddress->isStreet1Valid() ? '' : 'validation-error' ?>" />
                </div>
                <div>
                    <label>&nbsp;</label>
                    <input type="text" name="street2" id="street2" tabindex="3" value="<?=$invoice->customer->billingAddress->street2?>" />
                </div>
                <div>
                    <label for="city" class="req">City</label>
                    <input type="text" name="city" id="city" tabindex="4" value="<?=$invoice->customer->billingAddress->city?>" class="<?=$noValidation || $invoice->customer->billingAddress->isCityValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="zip" class="req">ZIP</label>
                    <input type="text" name="zip" id="zip" tabindex="5" value="<?=$invoice->customer->billingAddress->zip?>" class="<?=$noValidation || $invoice->customer->billingAddress->isZipValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="state" class="req">State</label>
                    <select name="state" id="state" tabindex="6" class="<?=$noValidation || $invoice->customer->billingAddress->isStateValid() ? '' : 'validation-error' ?>">
<?
        while (list($abbrev, $name)=each($states)) {
                printf("<option %s value=\"%s\">%s</option>\n", ($invoice->customer->billingAddress->state==$abbrev) ? 'selected="selected"' : '', $abbrev, $name); 
        }
?>
                    </select>
                </div>
                <div>
                    <label for="email" class="req">Email</label>
                    <input type="text" name="email" id="email" tabindex="7" value="<?=$invoice->customer->email?>" class="<?=$noValidation || $invoice->customer->isEmailValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="phone" class="req">Phone</label>
                    <input type="text" name="phone" id="phone" tabindex="8" value="<?=$invoice->customer->phone?>" class="<?=$noValidation || $invoice->customer->isPhoneValid() ? '' : 'validation-error' ?>"/>
                </div>
            </fieldset>
        </div>
        <div id="shipping">
            <fieldset>
                <legend>Shipping Information</legend>
                <div>
                    <label class="checkbox">
                        <input onclick="toggle()" class="checkbox" type="checkbox" tabindex="9" name="shipToBillingAddress" <?= ($invoice->shipToBillingAddress ? 'checked="checked"' : '')?> />
                        Same as billing address
                    </label>
                </div>
                <div>
                    <label for="shipToName" class="req">Name</label>
                    <input type="text" name="shipToName" id="shipToName" tabindex="10" value="<?=$invoice->shippingAddress->name?>" class="<?=$noValidation || $invoice->shipToBillingAddress || $invoice->shippingAddress->isNameValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="shipTostreet1" class="req">Address</label>
                    <input type="text" name="shipTostreet1" id="shipTostreet1" tabindex="11" value="<?=$invoice->shippingAddress->street1?>" class="<?=$noValidation || $invoice->shipToBillingAddress  || $invoice->shippingAddress->isStreet1Valid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label>&nbsp;</label>
                    <input type="text" name="shipTostreet2" id="shipTostreet2" tabindex="12" value="<?=$invoice->shippingAddress->street2?>" />
                </div>
                <div>
                    <label for="shipTocity" class="req">City</label>
                    <input type="text" name="shipTocity" id="shipTocity" tabindex="13" value="<?=$invoice->shippingAddress->city?>" class="<?=$noValidation || $invoice->shipToBillingAddress  || $invoice->shippingAddress->isCityValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="shipTozip" class="req">ZIP</label>
                    <input type="text" name="shipTozip" id="shipTozip" tabindex="14" value="<?=$invoice->shippingAddress->zip?>" class="<?=$noValidation || $invoice->shipToBillingAddress  || $invoice->shippingAddress->isZipValid() ? '' : 'validation-error' ?>"/>
                </div>
                <div>
                    <label for="shipTostate" class="req">State</label>
                    <select name="shipTostate" id="shipTostate" tabindex="15" class="<?=$noValidation || $invoice->shipToBillingAddress  || $invoice->shippingAddress->isStateValid() ? '' : 'validation-error' ?>">
<?
    reset($states);
    while (list($abbrev, $name)=each($states)) {
            printf("<option %s value=\"%s\">%s</option>\n", ($invoice->shippingAddress->state==$abbrev) ? 'selected="selected"' : '', $abbrev, $name); 
    }
?>
                    </select>
                </div>
            </fieldset>
        </div>
    </form>
    <script language="JavaScript" type="text/javascript"><!--
toggle();
-->
    </script>
<? } ?>
</div>

<? include "footer.php" ?>


De form welke hierin zit heb ik als action (wanneer ik de rewrite rule gebruik) als # of helemaal leeg. Als ik de pagina in een eigen script include en dus ?page=invoice.php als /invoice aanroep via een Rewrite Rule werkt de check voor de fields niet, dus de "if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {" op regel 38 wordt " overgeslagen".

Wanneer ik de pagina als pagina aanroep werkt het prima !

Ik haal trouwens wel alle includes weg omdat die in mijn eigen baksel geinclude staan, dus maak je niet druk ;)

[ Voor 100% gewijzigd door Verwijderd op 27-11-2008 22:34 ]


  • Noork
  • Registratie: Juni 2001
  • Niet online
Verwijderd schreef op donderdag 27 november 2008 @ 22:33:
Ik vind het altijd lastig om hele lappen code neer te zetten, ik wil het graag oplossen zonder het idee te creeren van " ik kwak het neer en lossen jullie het maar op".
Ik gaf aan 'relevante' code te plaatsen. Dus niet je table layout, javascripts etc. Gewoon de form declaratie, en hoe je het in php afvangt.

[edit]
Zoals dit dus:

PHP:
1
2
3
4
5
6
7
8
9
10
<form id="naam_form" method="post" action="<?=$_SERVER['PHP_SELF']; ?>" enctype="application/x-www-form-urlencoded"> 
    <input type="text" name="name" /> 
    <input type="submit"/>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
    print_r($_POST);
}
?> 


Je zou ook b.v. jouw "$_SERVER['REQUEST_METHOD'] == 'POST'" statement kunnen vervangen door b.v. if of isset($_POST).

Zie verder dit om te checken op het bestaan van variabelen: http://www.killersoft.com/misc/php_variable_tests.php

[ Voor 41% gewijzigd door Noork op 27-11-2008 23:17 ]


Verwijderd

Topicstarter
Noork schreef op donderdag 27 november 2008 @ 22:55:
[...]

Ik gaf aan 'relevante' code te plaatsen. Dus niet je table layout, javascripts etc. Gewoon de form declaratie, en hoe je het in php afvangt.
Mijn excuus ! :+
[edit]
Zoals dit dus:

PHP:
1
2
3
4
5
6
7
8
9
10
<form id="naam_form" method="post" action="<?=$_SERVER['PHP_SELF']; ?>" enctype="application/x-www-form-urlencoded"> 
    <input type="text" name="name" /> 
    <input type="submit"/>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
    print_r($_POST);
}
?> 


Je zou ook b.v. jouw "$_SERVER['REQUEST_METHOD'] == 'POST'" statement kunnen vervangen door b.v. if of isset($_POST).

Zie verder dit om te checken op het bestaan van variabelen: http://www.killersoft.com/misc/php_variable_tests.php
Ja was ook mijn idee, echter vraag ik me af waarom het wel zou werken wanneer het bestand direct aangeroepen wordt.

Dit vind ik toch vreemd.

Ik heb wel wat negatieve posts gevonden over de $_SERVER['REQUEST_METHOD'] == 'POST' omdat deze niet altijd gelijk zou hoeven zijn aan POST.

Ik ga toch de weg in welke jij ook aangeeft, voel me er wat beter bij.. wilde het toch even weten !

  • Spider.007
  • Registratie: December 2000
  • Niet online

Spider.007

* Tetragrammaton

Noork schreef op donderdag 27 november 2008 @ 22:55:
[...]

Je zou ook b.v. jouw "$_SERVER['REQUEST_METHOD'] == 'POST'" statement kunnen vervangen door b.v. if of isset($_POST).

Zie verder dit om te checken op het bestaan van variabelen: http://www.killersoft.com/misc/php_variable_tests.php
$_POST is altijd gezet; dus dat lijkt me niet erg verstandig. ;)

Ik snap na al deze informatie nog steeds niet wat nou het probleem is; zou het jezelf ook niet helpen om een geïsoleerde testcase te maken? Als ik bovenstaande htaccess en form include gaat alles gewoon goed; als ik naar /whatever toega kom ik in de index.php terecht; request_methode = GET en _GET['page'] = 'whatever'. Als ik vervolgens via bovenstaand formulier post naar /whatever kom ik ook in de index.php terecht, request_methode = POST, _GET['page'] = 'whatever' en _POST['name'] = 'dinges'.

Wat is er anders aan mijn setup; of wat klopt er aan bovenstaande niet volgens jou?

---
Prozium - The great nepenthe. Opiate of our masses. Glue of our great society. Salve and salvation, it has delivered us from pathos, from sorrow, the deepest chasms of melancholy and hate

Pagina: 1