Connecting Tech Pros Worldwide Forums | Help | Site Map

new to PHP

Stewart Robert Hinsley
Guest
 
Posts: n/a
#1: Jul 17 '05
I've just finished my first PHP script (fronted at <URL:http://www.malva
ceae.info/Index/Vernacular/index.html).

One thing is puzzling me - when parsing the query string

split("&", $QUERY_STRING) works, but
split('&', $QUERY_STRING) doesn't, yet

split('=', <string>) does/

What subtlety am I missing?
--
Stewart Robert Hinsley
http://www.malvaceae.info
http://www.meden.demon.co.uk

Phil Roberts
Guest
 
Posts: n/a
#2: Jul 17 '05

re: new to PHP


With total disregard for any kind of safety measures Stewart
Robert Hinsley <{$news$}@meden.demon.co.uk> leapt forth and
uttered:
[color=blue]
> I've just finished my first PHP script (fronted at
> <URL:http://www.malva ceae.info/Index/Vernacular/index.html).
>
> One thing is puzzling me - when parsing the query string
>
> split("&", $QUERY_STRING) works, but
> split('&', $QUERY_STRING) doesn't, yet
>
> split('=', <string>) does/
>
> What subtlety am I missing?[/color]

Why are you doing that in the first place? PHP automatically
converts query string values for you.

In any case, you should be using explode() rather than split() as
it is faster. split() uses regex parsing which carries an extra
overhead.

Would I be correct in assuming that you are coming from a Perl
background?

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Chung Leong
Guest
 
Posts: n/a
#3: Jul 17 '05

re: new to PHP



"Stewart Robert Hinsley" <{$news$}@meden.demon.co.uk> wrote in message
news:tzU36XALutjAFw0v@meden.demon.co.uk...[color=blue]
> I've just finished my first PHP script (fronted at <URL:http://www.malva
> ceae.info/Index/Vernacular/index.html).
>
> One thing is puzzling me - when parsing the query string
>
> split("&", $QUERY_STRING) works, but
> split('&', $QUERY_STRING) doesn't, yet
>
> split('=', <string>) does/
>
> What subtlety am I missing?
> --[/color]

Don't know, but there's parse_str() so you don't need to parse the query
string yourself.


Stewart Robert Hinsley
Guest
 
Posts: n/a
#4: Jul 17 '05

re: new to PHP


In article <Xns94D8F095AEF02philroberts@216.196.97.132>, Phil Roberts
<philrob@HOLYflatnetSHIT.net> writes[color=blue]
>With total disregard for any kind of safety measures Stewart
>Robert Hinsley <{$news$}@meden.demon.co.uk> leapt forth and
>uttered:
>[color=green]
>> I've just finished my first PHP script (fronted at
>> <URL:http://www.malva ceae.info/Index/Vernacular/index.html).
>>
>> One thing is puzzling me - when parsing the query string
>>
>> split("&", $QUERY_STRING) works, but
>> split('&', $QUERY_STRING) doesn't, yet
>>
>> split('=', <string>) does/
>>
>> What subtlety am I missing?[/color]
>
>Why are you doing that in the first place? PHP automatically
>converts query string values for you.[/color]

That's what I thought, but it didn't seem to work. Now I'm past the
steep part of the learning curve, I can experiment some more.[color=blue]
>
>In any case, you should be using explode() rather than split() as
>it is faster. split() uses regex parsing which carries an extra
>overhead.[/color]

Point taken.[color=blue]
>
>Would I be correct in assuming that you are coming from a Perl
>background?
>[/color]
More or less (previously FORTRAN, Pascal, CORAL, C and C++).
--
Stewart Robert Hinsley
Terence
Guest
 
Posts: n/a
#5: Jul 17 '05

re: new to PHP


Phil Roberts wrote:
[color=blue]
> With total disregard for any kind of safety measures Stewart
> Robert Hinsley <{$news$}@meden.demon.co.uk> leapt forth and
> uttered:
>
>[color=green]
>>I've just finished my first PHP script (fronted at
>><URL:http://www.malva ceae.info/Index/Vernacular/index.html).
>>
>>One thing is puzzling me - when parsing the query string
>>
>>split("&", $QUERY_STRING) works, but
>>split('&', $QUERY_STRING) doesn't, yet
>>
>>split('=', <string>) does/
>>
>>What subtlety am I missing?[/color]
>
>
> Why are you doing that in the first place? PHP automatically
> converts query string values for you.
>[/color]

Phil is correct. PHP has a solution for accessing varaibles passed in
the URL. It makes them available as an associative array called $_GET

This array variable is called a super-global. PHP also provides other
superglobals to make accessing the environment variables easier.

check them out here
http://www.php.net/reserved.variables

$_SESSION is particularly cool, but you need to run the session_start()
function first before it works. Unfortunately the above page neglects to
mention that and it has caught some people out.

As a newbie, you will want to make sure you get to know PHP's session
handling capabilities -- IT WILL SAVE YOU A LOT OF WORK!
http://www.php.net/manual/en/ref.session.php
remember that before the $_SESSION superglobal came along, it was
neccesary to use the other session functions all the time, now you can
do things like.

<?php

session_start();

if(!isset($_SESSION["uname"])) {
if(blnAutheticated($_POST)) { // use form data
$_SESSION["uname"] = $_POST["uname'];
}
else {
header("Location: login.php"); // redirect them
die();
}
}
echo "You are currently logged in as ".$_SESSION["uname"];
?>

obviously the above code requires you to write a blnAutheticated()
function which accepts an associative array that it will search for
authentication tokens.

No fartin' about with cookies and what not...

hmmm... I seem to have gotten a bit side-tracked...
------------ And now a word from our sponsor ------------------
Want to have instant messaging, and chat rooms, and discussion
groups for your local users or business, you need dbabble!
-- See http://netwinsite.com/sponsor/sponsor_dbabble.htm ----
Dan Tripp
Guest
 
Posts: n/a
#6: Jul 17 '05

re: new to PHP


Terence wrote:[color=blue]
> Phil is correct. PHP has a solution for accessing varaibles passed in
> the URL. It makes them available as an associative array called $_GET[/color]

Or $_POST if your form uses the POST method.

[color=blue]
> obviously the above code requires you to write a blnAutheticated()
> function which accepts an associative array that it will search for
> authentication tokens.
>
> No fartin' about with cookies and what not...[/color]

If you wanna fart around with cookies, though (what a weird phrase that
is! ;) ) the $_COOKIES is useful.

Cookies are ok, but session data is more than likely better. I'm not
gonna say "always better" because somebody or other will have some
example of a case where they're not... =)

Regards,

- Dan
http://www.dantripp.com/
Closed Thread