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.
Related
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
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.
I have this link:
www.something.com/index.php?page=teams
I then have this link:
Profile
I want to achieve is this link:
www.something.com/index.php?page=teams&profile=5
But I get this instead:
www.something.com?profile=5
The tricky part is that I can't just write the whole "path" like this:
Profile
Because it isn't always on the page 'teams'.
It might be a pretty stupid question, but i can't really figure it out.
You must include the page=teams parameter in the href.
Instead of
Profile
Something like
Profile
If that link is static you must need to update the html file.
But if page=anyvalue then you must generate the href link dynamically in your php code
for example:
Where $page is the current page, e.g $page="teams"
Well, you can just add it to the query string using http_build_query:
$newQueryString = http_build_query(['profile' => 5] + $_GET);
And then output it to your link:
Profile
Try something like this:
echo '<a href="'.{$_SERVER['SCRIPT_NAME']}.{$_SERVER['QUERY_STRING']}
.'&profile=5">Profile</a>
This makes use of $_SERVER to get the current page name (SCRIPT_NAME) and the existing query string (QUERY_STRING), and then appends "&profile=5" onto the end.
Well i have sort of fixed it.... i created a session called page, so i can access it everywhere. The thing is i can't get the variable $_GET['page'] because it's inside a included file called search.php, therefore i created the session.. But thanks for you suggestions guys!
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
Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);