Welcome Guest, Not a member yet? Register   Sign In
URI / URL Question, trying to build a dynamic function based on specific URL structure
#1

[eluser]domainsvault[/eluser]
I am creating a member system and after reading the guide here I am a little fuzzy about how to handle what I want to do which is.

go to mydomain.com/verify/xxxxx
the xxxxx being a randomly generated hash and associative ID with said hash example:

mydomain.com/verify/123456|ahjHeAKh39RFjkl2
where I will break it down using the pipe between the ID and Hash as a delimiter there after when they land on the page. My question is for this is

1) is using the URL like that possible? cause when they go to verify I still want it to load as an index() whether or not the ID|Hash is there

2) what is the best way to approach the above so I can determine if the ID|Hash is there or not and act accordingly with it.
#2

[eluser]davidbehler[/eluser]
Add this to your config/route.php:
Code:
$route['verify/(:any)'] = 'verify/index/$1';

Then in your verify controller you could do something like this
Code:
class Verify extends CI_Controller {
  function __construct()
  {
    parent:__construct();
  }

  function index($code = FALSE)
  {
    if($code === FALSE) {
      // no code
    } else {
      $tmp = explode('|', $code);
      if(count($tmp) == 2) {
         // code exists in valid format
      } else {
        // code exists but not in valid format
      }
    }
  }
}

That's totally untested but should work. In case you are not using CI 2.0 but an older version you have to extend Controller instead of CI_Controller.
#3

[eluser]domainsvault[/eluser]
Yea that looks like it could work :-)
Thank you, I would have never thought about the routes bit.
#4

[eluser]exit[/eluser]
If you don't feel like using an own controller just to verify you can of course route somewhere else. You can check the userguide for more detailed instructions.

Another simple way of dealing with this is to instead of using function index()

You might want to just:

Code:
public function _remap()
{
   $hash = $this->uri->segment(2);
}

You should of course put some security on that too.

But if I were you i would go with a routing solution.
#5

[eluser]domainsvault[/eluser]
Im liking the route concept as it gives me the added security I want. But I am curious am I able to have more than one of those route like instances as mentioned above in the config or will that conflict some how?
#6

[eluser]domainsvault[/eluser]
nevermind I answered my own last question..
#7

[eluser]domainsvault[/eluser]
New question a complication of sorts that has arisen from apply the route concept. I have constructed a generalize template for my site that doesn't use the template parser as the template we need under this and a couple other projects is a little more complex than the parser allows, that said I know this issue isn't specific to the template or the coding surrounding it.

After applying the route for the URL i wanted to use I noticed every time I go to that specific page I get syntax errors from my javascript.

if I go to mydomain.com/verify its fine no syntax error, i go to mydomain.com/anythingelse I am fine.. i go to
mydomain.com/000000:a77ced7bf3e9 (example of course any numeric value : hash value applies) (also changed the pipe to a colan as FF wasn't liking that where as other browsers didnt mind)

Anyway, I guess the question at hand is after saying that is, what could be causing this?
#8

[eluser]domainsvault[/eluser]
Just for giggles, i tried this concept:
Code:
public function _remap()
{
   $hash = $this->uri->segment(2);
}

all I get is a white screen, go figure..

Going the way of:
Code:
$route['verify/(:any)'] = 'verify/index/$1';

seems to be my best bet. However any JavaScript pulled into the pages template gets syntax errors now. I have tried everything I can think of and find to resolve it but I gotta say I'm stuck. The syntax errors that the JavaScript are throwing up are missing ; on line one, referring to the sites actual template file rather than any other specific file. Issue with this is this same template same JavaScript is called in everywhere the same exact way. and works without error everywhere else but the page effected by the routes, and it only effects the page when there is a second segment but not if there isn't one.

so in my case if i go to "domain.com/verify/" works smooth no error i go to "domain.com/verify/100002-74c418fd4572ee437929349cd996eac9" and it causes the JavaScript to error. I am open to suggestions as I am stuck, and a lot of what I am doing is AJAX driven so opting out of the JavaScript is not an option.
#9

[eluser]exit[/eluser]
There are built in helper for this such as img(), link_tag(), anchor() etc. But if you want to you can use base_url() for linking to files on your webserver (use site_url() for linking to CI pages).

Example of linking to a javascript file:
Code:
< script type="text/javascript" src="<?=base_url()?>include/js/example.js">< /script>

Example of linking to an image file:
Code:
< img src="&lt;?=base_url()?&gt;images/example.png" alt="example" />

Example of a <a> CI link:
Code:
< a href="&lt;?=site_url()?&gt;archives/news">News archive</a>

Example of a <a> link to a lokal .php or .html file on the webserver
Code:
< a h ref="&lt;?=base_url()?&gt;include/example.php">Example</a>

The reason for using site_url() instead of base_url() is quite simple.
site_url() will include the index_page you've chosen in config.php. If you are using mod_rewrite and
Code:
$config['index_page'] = '';
both functions will return the same result, but it's best to keep it correct in case of future changes.
#10

[eluser]domainsvault[/eluser]
the base_url solved the issue. Thank you




Theme © iAndrew 2016 - Forum software by © MyBB