I'd like to pass a URL like:

And whoever gets this URL should be able to open a page and view the page associated with that code.

Need help with the steps and fill some gaps in my understanding of the process.

  1. I assume I need to create: index.php under dir/
  2. Somehow I need to retreive the code from the last part of my URL and trun it into a var.

I think once i get the var I can do the rest.

My code will always be an 8 char string.

I think it's also possible with .htaccess rewrite somehow, but not sure if i want to take that route... But if it is a better way to do it with rewrite then perhaps I need to skip my /dir/ in the URL and just do:

and once the URL is clicked use header to pull the page from http://mydomain.com/dir/

A bit foggy on the whole procedure.
Suggestions?

    .htaccess would be easier:

    1.) You set up a general RegEx to get the code:
    mydomain.com/dir/([a-zA-Z0-9]{8})

    2.) You redirect to your page with this code as a var in the URL:
    mydomain.com/dir/index.php?code=$1

    3.) Index.php now takes the var $_GET['code'] and gets the code....

      I think I'm starting to understand it better.

      However I don't want to redirect to another URL.

      Ideally I'd like to check if mydomain.com is followed by UPPERCASE 8-char long code from $_GET request and if yes, check if there's such code in my db.

      If yes, include file1.php, else show an error message.

      How can I do that?

        Well, that's difficult. You'd have to rewrite the URL so that you can get that code... however, unfortunately it's impossible to do exactly as you want, since you're delineating a subdirectory of your root. Unless you have a folder for each Database entry, you're going to have to redirect. But your users won't notice it, at least not until after the page shows.

          OK, then -- I need to give it a shot.

          I'm really weak on RegEx. So I'm not sure how to do the item #1 on your list.

          I can use something like:

          $url = 'http://mydomain.com/dir/ABCDEFGH';
          
          $targetURL = parse_url($url);
          
          $mycode = basename($targetURL['path']);
          
          // then do redirect with header?..
          

          But something tells me this is somehow backwards, and it's gotta be a more elegant solution...

            I think I need to run a quick check to see if $myvar is

            1. 8 chars long
            2. All alpha
            3. All Caps

            and if all these conditions are met hit the db to see if such code is actually present.

              Use .htaccess

              RewriteEngine On
              RewriteCond %{HTTP_HOST} ^domain\.com/dir
              RewriteRule ^/([A-Z]{8}) http://domain\.com/index\.php?code=$1

              Now, in your index.php you can just get the code with: $_GET['code']

              You can validate inside your php file:

              if(ereg('/[^A-Z]/', $_GET['code'])) {
                // We have non-uppercase characters, or numbers / non-alpha characters
              } else {
                // We have all upper case characters
                if(strlen($_GET['code']) == 8) {
                  // We have an 8 character string
                } else {
                  // We have more or less than 8 characters
                }
              }

              You can combine that into:

              if(ereg("^[^A-Z]*$", $_GET['code']) && strlen($_GET['code']) != 8) {
                // Doesn't fit our criteria
              }
              else {
                // We're good
              }

                Great! Lemme give it a whirl!

                BTW, I goofed with something and sent you a PM.

                  I have no idea what am I doing wrong, bu the redirect simply does not work...

                  How do I check in the engine is on?

                    Write a Reply...