Closed Thread Icon

Topic awaiting preservation: php url check then echo acordingly.. (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12552" title="Pages that link to Topic awaiting preservation: php url check then echo acordingly.. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: php url check then echo acordingly.. <span class="small">(Page 1 of 1)</span>\

 
Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 12-12-2002 22:55

Basicly i want to check what's in their url bar and change a small section of content based upon it

For example if "http://site.com/show.php?id=1" is entered into the browser then echo 'lalalalala';

and if "http://site.com/show.php?id=2" is entered into the browser then echo 'blahblahblah';

Yeah I know this is easy but i can't seem to keep my brain turned on long enough to figure it out, i've done it before but that was then, and this is now...

Thanks for any help in advance


Lurch
Paranoid (IV) Inmate

From: Behind the Wheel
Insane since: Jan 2002

posted posted 12-13-2002 00:09

the "id=1" in your url will be a php variable in the page - a url encoded variable in fact.

so now you can use "id" as you would any other variable in php - on that page. So in the php, you could just do something like:

code:
if($id == "1"){
echo("lalalalala");
}elseif($id == "2"){
echo("doobeedoobeedoo");
}



so you could use if/else statements, switch statements, or whatever you want hope that helps



[This message has been edited by Lurch (edited 12-13-2002).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 12-13-2002 00:29
code:
[url=http://www.php.net/manual/en/control-structures.switch.php]switch[/url] ($id) {
case 1:
$blah = "Something";
break;
case 2:
$blah = "Something else";
break;
default:
}
echo $blah;




[This message has been edited by Pugzly (edited 12-13-2002).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-13-2002 00:53

If register_globals is turned off (the default for newer versions of PHP), you'll have to

$id = $_GET["id"];

before you can access $id as a normal variable.

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 12-13-2002 02:27

Wow thaks so much guys, exactly what I needed

Ok now about the choice of if/else statements or switch statements, it works great either way, but is one way more proper than the other, or might one of them be better when doing large amounts queries? I mean what's the pros/cons type stuff? I also heard about using an "array" instead, any thoughts on that?

Oh and Perfect Thunder, thanks for the tip about the register_globals. They were indeed turned off on my server but I went and turned it on. Is there side effects to that i should consider?

*Sorry for all the questions, but I like to try and learn from others when ever possible*

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-13-2002 07:38

Definitely! If register_globals is turned on, then ANYTHING you put in your querystring becomes a variable in your script!

Let's say you have a script called admin.php -- it has a lot of functions related to site maintenance. It checks a user's password and username and if they match, it finds out his level of privileges. 1 would be a normal user, 2 would be a moderator, 3 would be an admin, 4 would be the superuser. The user level, once determined, is stored in a variable $user_level.

Now let's pretend your script allows the superuser to delete pages. Perfectly reasonable, right?

Now let's say that a malicious user accesses your script using the url: www.yoursite.com/admin.php?user_level=4

If register_globals is turned on, then every variable in the querystring is automatically turned into a script variable... and suddenly this bozo has the ability to delete your entire site.

Now, this example assumes that you've coded in a certain way, and it assumes that someone else knows what variables do what... but you have to admit that it's a pretty big potential security hole.

It's better to leave register_globals turned off, and manually get only the querystring variables you expect. Say the querystring is supposed to look like this:
www.yoursite.com/admin.php?username=bill&color=blue

Your script begins with

$username = $_GET["username"];
$color = $_GET["color"];

Then if someone types something like
www.yoursite.com/admin.php?username=bill&color=blue&secretvariable=evilvalue

Then only $username and $color will be defined. And you're safe!

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 12-13-2002 19:03

Thanks for all the info Perfect Thunder, i'll go turn that back off now

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-13-2002 21:03

The choice between Switch and if/else is mostly personal they will do the same thingsb



.:[ Never resist a perfect moment ]:.

Rooster
Bipolar (III) Inmate

From: the uterus
Insane since: Nov 2002

posted posted 12-14-2002 12:57

The decisions between using things such as if/else, switch, or pre-initialized data is very dependent on what you are trying to accomplish at the time.

If/else structures are more for areas that lack the organization needed for a switch statement.

Switch is a good control structure for things that have a single element that need to matched against multiple conditions.

Pre-initialized data should be used for areas that have numerous or an unknown number of elements. The trade-off is between using the processor, or allocating more memory. Pre-initialized data will not be burdened with the conditional testing of the standard control structure but memory is needed to create the array, or list, or tree, or map or whatever your end up using. Pre-initialized data is chosen when the processing overhead for the needed control structures becomes too extensive for the desired implementation (i.e. a lot of elements).

Control structures should be used as sparingly as possible. If there is anyway to write a concrete algorithm for what your trying to accomplish it will usual be a better choice; both from a processing standpoint and from a software engineering standpoint. However, if the concrete algorithm is forcefully developed just for the sake of not using a control structure (i.e. badly written) it will become the worst choice to use. Rational databases are a prime example of concrete algorithms. Instead of simply using control structures for data testing throughout tables, functionality is based on a type of algebra. Variables are gathered until a solvable algebraic equation can be created, then the equation is simply solved using mathematics.

Is that what you want to know?



::
~Existence is a mere pattern.~
::

abb
Bipolar (III) Inmate

From: Victoria, BC
Insane since: Mar 2002

posted posted 12-17-2002 23:01

Forgive me if i'm wrong, i just started learning php, but...

I use the parse_str($_SERVER["QUERY_STRING"]);
Which takes the query string (a=1&b=2) and makes them variables
($a=1 and $b=2)

if you put that as the first line of your script, and you initialize your variables first,
imho, the user can't mess with your script...

well, I hope they can't, cuz that's what I used on my scripts...

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 12-20-2002 20:13

Thanks guys, yes Rooster that is what I wanted to know

Right now, the if/else is what I need, but I have a project to start on later that would benifit from the switch statment menthod. Thanks again

« BackwardsOnwards »

Show Forum Drop Down Menu