Welcome Guest, Not a member yet? Register   Sign In
passing URLs to methods!
#1

[eluser]Fierymind[/eluser]
Hi,

I have a method on a controller that take 2 variables to track web page visitors.

Code:
function ($current_page, $referrer_page) { ...

$referrer_page is generated by javascript document.referrer

so, if website visitors come from google search page, $referrer_page will be something like:

Code:
http://www.google.com/search?hl=en&safe=off&&sa;=X&ei=rxgtTKrrHpXNjAeUlfnsDg&ved=0CBwQvwUoAQ&q=referrer&spell=1


how to put such variable on CI URI? I tried javascript functions escape() and encodeURIComponent() and they does not work.


Is there a way to convert document.referrer by javascript to some common format without any special chars that is readable by PHP?

like 'google.com/search?q=good' to be 'ABCS..' by Javascript so I pass it to CI URI and PHP return it back to 'google.com/search?q=good'?
#2

[eluser]Georgi Budinov[/eluser]
Hi, I am not sure I can understand what exactly you are trying to do but I think that the server variable could be handy here - $_SERVER['HTTP_REFERER']. If not please tell us what exactly you are doing and why you need javascript document.referer.
#3

[eluser]Fierymind[/eluser]
I have solved the issue, let me explain again the problem and solution .. may be this solve someone issue (or someone come with better solution).

tracking controller is part of larger software, we need to track visitors on static websites, from where the visitor come and what pages his visited and how often he come back.

my solution is embed the following code in each page of such static websites, its [removed]

Code:
page = document.location.href;
    from = document.referrer;
        if (from == '') from = 'unknown';

    document.wrte("<img style='display:none' src='http://www.domain.com/CI_installation/track/visit/"+page+"/"+from+"'>");

like you see, I pass the wanted info from Javascript to CI installation, but what if from variable equal to this:

Code:
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=something

obviously such string on CI URI will not work, escape() and encodeURIComponent() will not work too.

so what I can do? .. if I used $_SERVER[‘HTTP_REFERER’] on track/visit .. it will not return the correct value I'm looking for.



and here is my solution:

In Javascript, I'll convert every character of current URL and referrer URL to the equivalent ASCII code, separated by a dash.

Code:
function toASCII(input) {
         var output = '';

            for(i=0; i&lt;input.length; ++i)
                {
                    if(output != "") output += "-";
                    output += input.charCodeAt(i);
                }

          return output;
}

    page = document.location.href;
    from = document.referrer;
        if (from == '') from = 'unknown';
    page = toASCII(page);
    from = toASCII(from);
    document.wrte("&lt;img style='display:none' src='http://www.domain.com/CI_installation/track/visit/"+page+"/"+from+"'>");

then in my PHP part, I'll take this string (will be something like 75-46-21-67-..) and return the ASCII chars into readable text.
#4

[eluser]Simian Studios[/eluser]
Alternatively, you could put something like the following in your view and negate the need for JS altogether:

Code:
&lt;?php $this->load->helper('url');   // if not already loaded
$this->load->library('user_agent'); // if not already loaded  ?&gt;
&lt;img style="display:none" src="http://www.domain.com/CI_installation/track/visit/&lt;?php echo urlencode(current_url()); ?&gt;/&lt;?php echo urlencode($this->agent->referrer()); ?&gt;" /&gt;

Then you would just need to urldecode() the page and referrer...
#5

[eluser]Fierymind[/eluser]
but the track does not happen in PHP page, its static HTML files .. so I can't use such code, it must be 100% Javascript.
#6

[eluser]Simian Studios[/eluser]
Oh, fair enough then... Tongue
#7

[eluser]Georgi Budinov[/eluser]
I see now what your problem is: permitted_uri_chars.

encodeURIComponent is working as expected and it works for you too. You just have to do this:

Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?&=;';

But codeigniter suggests us to be more restrictive however. The suggestion here is working as I tested it with your example URI.




Theme © iAndrew 2016 - Forum software by © MyBB