Numbered URL in CodeIgniter PHP - php

I am making a new website and I want the URL to be both in numbered form as well as simple text form (just like stackoverflow.com 's URLS) but I am unable to do so without calling headers as I want that whenever the user visits using url like
http://MY_WEBSITE/654321
then it goes to, say the page is linked to abcd then the URL should be like
http://MY_WEBSITE/654321/abcd
But here both abcd and 654321 are dynamic as they will change across links.
So, how should I do this? How to change to to new URL without header
Any suggestions of implementing in a new way or fixing the problem is heartily welcomed!

This provides you for retrieving information from your URI strings
$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
example URL:
http://test.com/index.php/controller/action/1stsegment/2ndsegment
then it shows!
$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

Related

How to make pretty URLs using route?

I want pretty url. I access these records using url below. Last segment is index id to access data using it.
http://www.example.com/order/thank_you/344
How can I convert it into
http://www.example.com/order/thank_you
How to implement and also I want last segment in page to access the data using it.
routes.php
$route['order/thank_you/(:num)'] = 'thanks/thank_you/$1';
The left hand side is the URL accessed by the users. And the right hand side is where your request going to.
For example, the request example.com/order/thank_you/344 is going to the function thank_you() of the Controller thanks.
$1 is the argument. If have more than one, just /$1/$2/.
The Controller
public function friends_profile ($id = NULL) {
echo "This is the ID I want to thank to " .$id;
}
The function arguments can be passed directly by the route. $id is the argument passed by the URI.
By the way, check out the Codeigniter URI Routing Guide to get more information.
If you want the url not to contain the id portion you could keep it in a session var and retrieve it at the thank_you method.

How to generate a new URL using current_url in codeigniter

I am trying to use the current_url() function in codeigniter but not sure how to manipulate this data. I understand I can echo out the current_url() but how do I take each of the segments and use to generate a new URL?
I am trying to take pieces of the current page and use to generate a new URL with adding in new variable data.
I have never worked with current_url(); before, so not sure what to do. Lets say my user is on a filters page and has 2 variables to filter their results (neighborhood and category). If the user clicks on a neighborhood, how would I structure the hyperlink to pull the current url variable for the category and then insert the variable for neighborhood?
My URLs look something like:
mydomain.com/ny/find/$neighborhood/$category
Breakdown:
ny = controller
find = function
param1 = $neighborhood
param2 = $category
Any help would be much appreciated...
You can breakdown your URL by means of this:
For ny: echo $this->uri->segment(1);
For find: echo $this->uri->segment(2);
For param1: echo $this->uri->segment(3);
For param2: echo $this->uri->segment(4);
<?php //check if there is a value in uri segment 3 (param1)
if($this->uri->segment(3)){
//if there is a value there then include it in the link ?>
Link text
<?php //otherwise just go to the link for param1
}else{ ?>
Link text
<?php } ?>
This should work though depending on the exact logic needed you may have to tweak the links and also the logic in the else statements. Depending on how you want to link you may also have to add an elseif($this->uri->segment(4)) and use different logic if the user is on a page with 4 url segments.
current_url() returns a string for the actual page loaded. You can create any url you want using the segments.
Let´s say you have this current url:
mydomain.com/ny/find/$neighborhood/$category
if you want add something to this url and create a link to the user, you can make a string:
$url = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/newParamYouAdd/'.$this->uri->segment(4);
And then:
New url
Let me know if is this what you want.

Redirect users to Long URL

I have an application where URL after rewriting are like this
http://www.domain.com/product/seller/product_id
an example link would be
http://storeiown.com/product/kitchenking/92013
This was okay but I need the title of the product to be included in the url
http://storeiown.com/product/a-very-nice-electric-cooker-by-kitchenking/92013
I achieved this too and all was good.
Now, I want all the url which do not include the title to redirect to this one.
Like, if user lands from the url without the title they should be redirected to a version of the page with the url containing the title in the url.
How do i accomplish that. And for info additional info I use CodeIgniter in the app, if that makes it any easier.
you can do this way which i use. In the previous page type this at the top:-
<?php
session_start();
$_SESSION['mainpass']= '0';
?>
And in the next page code this at the top of the page :-
<?php
session_start;
if(isset($_SESSION['mainpass'])) {
//run the current page
}else{
header("location: www.domain.com");
}
?>
If you are using codeigniter, you could try the below.
$seg3 = $this->uri->segment(3);
if(is_numeric($seg3)){
//The user has come without the header because the third segment is numeric thus probably using the product id.
//Therefore, redirect again to the proper link after getting the heading from your db
} else {
// do nothing
// the seg3 is not numeric means it probably came through normal preferred way
}
And in order to use the
$this->uri->segment(3);
You need to either
auto load the url helper
load manually when required

GET url in Codeigniter

I am using codeigniter for my project. To get the uri segments I know I can use
$this->uri->segment();
but my case is a bit different
My url looks like
localhost/mediabox/home/box/21
but once I go to this url a popup form apears in which the user provide a key to access this page and I validate the key using ajax method which is inside my home controller validate_key function
when I echo the url it gives me localhost/home/validate_key
while calling valiate_key of the home controller how can I get the 21 from the url wrritten in the url bar?
Any ideas?
Thanks
The problem:
It's not a bug, it's a natural behavior.
Consider the following:
you request the validate_key function from the server by typing the URL in your address bar. current_url() returns localhost/blabla/validate_key. No AJAX involved.
requesting validate_key with AJAX. The same PHP code will be executed.
the current_url() will change to localhost/blabla/validate_key
even though your browser's address bar is showing localhost/blabla/box/21.
So, what does this means? It means the Codeigniter base_url() doesn't care about your address bar, it cares about the function it is in, whether it was called via ajax or normal request.
so as long this function is being executed, the URL is pointing to it.
The solution:
My favorite solution to such a case, is to simply create a hidden input.
Simply, when a user requests the box function. you're showing him a popup form. so add a hidden_input field, give it a name and a value of 21(depends).
For example(you should tailor this to your specific needs):
Add this to your form in the view that get displayed by the box function:
form_hidden("number", $this->uri->segment(3));;
Now these data will be sent to your validate_key function. How do we access it? It's simple!
function validate_key(){
$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
*Or , you can do the following it's considered much safer when you're ONLY
*expecting numbers, since this function(intval) will get the integer value of
*the uri segment which might be a destructive string, so if it's a string
*this function will simply return 0.
*/
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}
It looks like you've used .htaccess to remove the index.php part of the url. So, when you navigate to localhost/mediabox/home/box/21 you're passing the value 21 to the function named box in the controller named home
If you want to keep that value within the validate_key function, just pass it through when calling it:
function box($param)
{
//$param = 21
$this->validate_key($param);
}
Its better and suggetsed to use hidden field and post the value when it is needed.

Zend, How to access current route from within Action Helper

In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.
What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?
Is there a simpler way? (Set action requires the URI string as a parameter).
Thanks!
You can do as #Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:
$request = Zend_Controller_Front::getInstance()->getRequest();
echo $request->getHeader('referer'); // referer's address
echo $request->getRequestUri(); // current address
I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.
If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:
// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
$this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
$this->_redirect('/');
}
which is located in the login action (i.e. LoginController->loginAction()).

Categories