hide part of the url

Discussion in 'PHP' started by anurag.sharma, Jun 25, 2008.

  1. #1
    i use get function in some my php pages and it will display the url as site.com/index.php?id=xyz

    i wish to hide the "?id=xyz" portion from all the urls and display url simply as site.com/index.php

    What should i do to acheive this?
     
    anurag.sharma, Jun 25, 2008 IP
  2. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try `post` instead of `get`...
     
    bokiatenxi, Jun 25, 2008 IP
  3. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know that way. but i wish to use get funtion itself. any suggestion?
     
    anurag.sharma, Jun 25, 2008 IP
  4. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    bokiatenxi, Jun 25, 2008 IP
  5. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you want an easier way, try using sessions...
     
    bokiatenxi, Jun 25, 2008 IP
  6. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    dear bokiatenxi , thanks a lot for the info .. cud u explain the 2nd suggestion (ie, using sessions)?
     
    anurag.sharma, Jun 25, 2008 IP
  7. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    something like this...

    session_start();
    if(isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
    header("Location: index.php");
    exit;
    }

    what this does is after using get to go to index.php?id=xyz, you save the value of id to a session variable then redirect back to index.php (without the ?id=xyz). After this, use the session variable instead of the get variable.

    this is not hiding the url for it will still show, but it will redirect quickly to the clean URL before the page actually loads. I would not recommend this, for me this is a waste of code, but I think this is the only way besides URL rewriting to be able to hide the get variables. Like I said before, if you want to hide the data being passed, the most efficient way for me is to use post instead of get...
     
    bokiatenxi, Jun 25, 2008 IP
  8. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks
    but i am getting this error

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/xxxxxxx/public_html/yyyyy/index.php:4) in /home/xxxxxxx/public_html/yyyyy/index.php on line 5

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/xxxxxxx/public_html/yyyyy/index.php:4) in /home/xxxxxxx/public_html/yyyyy/index.php on line 5
     
    anurag.sharma, Jun 25, 2008 IP
  9. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    this is the actual code of index.php

    <html>
       <head><title>Welcome</title></head>
       <body>
    <?php
    session_start();
    if(isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
    header("Location: index.php");
    exit;
    }
    include "header.html";
    print (isset($_GET['name']) ? "\"" : "")."".(isset($_GET['name']) ? $_GET['name']."\"" : "name");
    include "footer.html";
    ?>
    PHP:

    Please help
     
    anurag.sharma, Jun 25, 2008 IP
  10. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    you need to put session_start() at the very first line of your code for it to work...

    Also, the header function will only work if you put it before any html codes, echo statements or print statements..

    this is how it should look...

    <?php
    session_start();
    if(isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
    header("Location: index.php");
    exit;
    }
    ?>
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <?php
    include "header.html";
    print (isset($_GET['name']) ? "\"" : "")."".(isset($_GET['name']) ? $_GET['name']."\"" : "name");
    include "footer.html";
    ?>
     
    bokiatenxi, Jun 25, 2008 IP
  11. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Also,

    print (isset($_GET['name']) ? "\"" : "")."".(isset($_GET['name']) ? $_GET['name']."\"" : "name");

    after redirection, the get variables are gone, so the above line will no longer output the get variables. you should also add the get variable 'name' in a session variable
     
    bokiatenxi, Jun 25, 2008 IP
  12. KevMuk

    KevMuk Guest

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    KevMuk, Jun 25, 2008 IP
  13. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks a lot
     
    anurag.sharma, Jun 25, 2008 IP
  14. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    but now another problem.

    It is simply printing "id" in the final redirected page (index.php). what should i do to display the contend (GET_funtion) in the final page
     
    anurag.sharma, Jun 25, 2008 IP
  15. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    tried

    session_start();
    if(isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
    print_r($_SESSION);
    header("Location: java.v");
    exit;

    but not getting desired results
     
    anurag.sharma, Jun 25, 2008 IP
  16. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    did you use $_SESSION['id']? the $GET variables will be gone after redirecting...
     
    bokiatenxi, Jun 25, 2008 IP
  17. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    here is code . Please do the correction


    <?php
    session_start();
    if(isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
    header("Location: index.php");
    exit;
    }
    ?>
    <html>

    <head><title>Welcome</title></head>
    <body>
    <?php
    print_r($_SESSION);
    include "head.html";
    print (isset($_GET['id']) ? "\"Welcome" : "")."".(isset($_GET['id']) ? $_GET['id']."\"" : "id");
    include "foot.html";
    ?>
     
    anurag.sharma, Jun 25, 2008 IP
  18. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    resolved the issue.. Thanks
     
    anurag.sharma, Jun 25, 2008 IP