Using GET

Discussion in 'PHP' started by enchance, Sep 30, 2007.

  1. #1
    How do you pass a variable using GET when a text link is pressed? The only difference is that it reloads the current page but this time with a new value for the variable so it would be like it passed the new value to itself. Like if it were a gallery, it would just refresh itself with a new picture depending on the text link pressed.

    Pardon if my question isn't all clear, I'm kinda new to php and am still trying to learn the ropes.
     
    enchance, Sep 30, 2007 IP
  2. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You would just use this format:

    <a href='yourpage.php?var1=bob&some_var=32fd1'>anchor text</a>

    -the mole
     
    themole, Sep 30, 2007 IP
  3. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #3
    This is how I do it...

    <a href="http://sitename.com/index.php?page=contact-us">Contact Us</a>

    <?php
    content = "content/" . $_GET["page"] . ".php";
    include "template.php"
    ?>

    and inside template.php
    <html>
    <head>
    </head>
    <body>
    <?php
    include $content;
    ?>
    </body>
    </html>

    you should make sure only certain values are allowed so spammers don't try to dig up a file from a different part of your site.
     
    clinton, Sep 30, 2007 IP
  4. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks! But what do you mean by "certain values?"
     
    enchance, Sep 30, 2007 IP
  5. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Basically someone could pass values through $_GET[page] that would allow them to access files that could compromise your system or get info you don't want them to get (eg, /etc/passwd your db login info file, whatever pretty much).

    You should change the first line of clinton's post to this:

    $content = 'content/'.basename($_GET['page']).'.php';

    basename will just return the filename from a path, so it would strip out all the other junk. This will cause problems if your file is in a subfolder in content/ since it's stripping out that subfolder.

    -the mole
     
    themole, Sep 30, 2007 IP
  6. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I see, thanks! I'll make sure to replace the line.
     
    enchance, Sep 30, 2007 IP
  7. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I ran a small test but the image won't display. Check it out:
    
    <?php
    //The name of the file is "main.php"
    
    if(isset($_GET['pic']))
    {
    	$thispic = $_GET['pic'];
    }
    else
    {
    	$thispic = 1;
    }
    
    $paths[1] = "orange.jpg";
    $paths[2] = "green.jpg";
    $paths[3] = "red.jpg";
    ?>
    
    <img src="<?php $paths[thispic]?>" />
    <a href="main.php?pic=1">Orange</a> <a href="main.php?pic=2">Green</a> <a href="main.php?pic=3">Red</a>
    
    Code (markup):
    Did I miss anything?
     
    enchance, Oct 2, 2007 IP
  8. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #8
    <?php $paths[thispic]?>

    should be

    <?php echo $paths[$thispic]; ?>
     
    krt, Oct 2, 2007 IP
  9. hemlata

    hemlata Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hello,

    Replace your following code

    <img src="<?php $paths[thispic]?>" />
    PHP:
    with

    <img src="<?php echo $paths[$thispic]; ?>" />
    PHP:
    Hope this will solve your issue.

    Regards,
     
    hemlata, Oct 3, 2007 IP
  10. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It worked! Thanks.
     
    enchance, Oct 3, 2007 IP
  11. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    addition

    you should screen all variable, so instead of

    you must ensure that get['pic'] only contain what you want them tyo be ( in your case, pic must be equal to 1 or 2 or 3 )

    so why not use

    is a longer code, and more writing, but if you get yourself used to it, you'll have arelatively more secure website.
     
    kendo1979, Oct 3, 2007 IP
  12. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Cool. I just thought about that. Thanks!
     
    enchance, Oct 7, 2007 IP