PHP passing parameters via URL

前端 未结 2 1952
我在风中等你
我在风中等你 2020-12-03 12:58

I want to know how I can pass parameters between pages through the URL without having to add variables eg:

mydomain.com/file.php?var1=val1&var2=val2&         


        
相关标签:
2条回答
  • 2020-12-03 13:28

    You should use .htaccess

    Here's an example:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [NC,L]
    

    This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.

    Example:

    mysite.com/20/this_is_a_new_article/
    

    Would actually mean

    mysite.com?var1=20&var2=this_is_a_new_article
    

    Of course, in your index.php file you can simply take the values using

    $var1 = $_GET['var1'];
    $var2 = $_GET['var2'];
    

    Cheers!

    0 讨论(0)
  • 2020-12-03 13:35

    Your third example is an example of how REST identifies a server side resource. What you are talking about here sounds very much like REST will do what you want. I would suggest starting here (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services).

    0 讨论(0)
提交回复
热议问题