Welcome Guest, Not a member yet? Register   Sign In
Can CI grab URL Hash (#page anchor) like uri_segment grabs segments ???
#1

[eluser]charlie spider[/eluser]
for example:

A form on a CMS page www.mysite.com/edit_page/about-us#seo

posts data to a controller for processing, database update/insertion, etc, but then afterwards, needs to redirect back to the above URL.

How do I use CI to grab the hash at the end of the above URL, as the hash does not appear within the uri_segment_array ?

Thank you in advance for your time, effort and talent.
#2

[eluser]charlie spider[/eluser]
wow

i thought this was a simple one but it looks like i've stumped a few of you at least.

any ideas?

thanks
#3

[eluser]Cro_Crx[/eluser]
You could do it with regex with a single line. For something more readable you can do this

Code:
/* Get the total number of segments */
$num_segments = $this->uri->total_segments();
/* Get the entire last segment*/
$last_segment = $this->uri->segment($num_segments);
/* Get the last hash in the segment. */
$last_hash = strrpos($last_segment, '#');

/* Now we just grab our anchor*/
$anchor = substr($last_segment, $last_hash + 1);

The $last_hash value will be false if there isn't one. Although when checking you'll need to use === instead of just ==
#4

[eluser]Cro_Crx[/eluser]
Oh wait sorry. I misread. If the hash doesn't appear in the $last_segment you can use the URL held within the $_SERVER global. I'm not sure what index it is. Just use print_r() to find it. The code would look something like this:

Code:
/* Get the entire URL */
$url = $SERVER['INDEX'];
/* Get the last hash in the URL. */
$last_hash = strrpos($url, '#');

/* Now we just grab our anchor*/
$anchor = substr($url, $last_hash + 1);

Just replace 'INDEX' with whatever the real index of the URL data is.
#5

[eluser]charlie spider[/eluser]
Thanks for the reply Cro-Crx,

a few things though:

1) There's no such thing as
Code:
$_SERVER['INDEX']

2)
Code:
$_SERVER['HTTP_REFERER']
$_SERVER['REQUEST_URI']
and
$_SERVER['QUERY_STRING']
all return the URL without the hash

which is why...

3) i wanted to know if CodeIgniter has yet more magic up it's sleeves that can parse the hash from a URL

i already tried what you had suggested anyways. I'm certain it can be done with javascript, but i'd like to stay server-side (and javscript feels like cheating anyways)
#6

[eluser]Cro_Crx[/eluser]
Ya, I was saying that I wasn't sure of the index, so just replace INDEX with whatever it is in the $_SERVER array. Although as you said, it's not in any of the 3 that you've listed so you'll have to use JavaScript to read it.

Reading up a bit about the topic. Apparently browsers don't send the anchor to the server, in that case it's not possible to recover.
#7

[eluser]charlie spider[/eluser]
ergh...

oh.. and thanks for the help




Theme © iAndrew 2016 - Forum software by © MyBB