External Links in CodeIgniter - codeigniter

I have the following code:
<div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div>
Which takes a users input for a link ($link) and puts the url into an anchor tag. It, however, is not redirecting to the external link but simply amending the base url for the site with the stored URL. I attempted to add 'http://' to the beginning of the submitted link which works unless the user has already supplied http in the link input. Any advice on how to overcome this would be amazing.

Yes, per the documentation, anchor() creates links based on your site's URL.
If things are working as expected when URL's are prefixed with http://, but you're having trouble with users sometimes adding http:// and sometimes not, you could simply check the link to determine whether it's ok, or if you need to prefix it. Here's a basic example using strpos:
if(strpos($link, 'http') === FALSE){
// link needs a prefix...
$link = 'http://' . link;
} else {
// link is ok!
}
...use CodeIgniter's prep_url() function (thanks to #cchana for reminding me of it!):
This function will add http:// in the event that a scheme is missing from a URL. Pass the URL string to the function like this:
$url = "example.com";
$url = prep_url($url);

Related

CodeIgniter URL Encryption

All Viewers I am New in Codeigniter, I need your guide to done my work, I want to Encrypted full URL like below example.
For example this is my url www.example.com & my controller is home, so full url is www.example.com/home
now I want to encrypted all controller, function like below
www.example.com/5115784bef2514430e7f74d9a71d4142a942efb0f7cc428626bda7633326f9d015fbacc60d93cd6b858f9b6e05c1e56263acb24297cecc720467eb4f222d81e5hdn5B
I can encrypted & decrypted the text well, but I just don't get how can I decrypted from url & make understand which controller or function its called, I want to decrypted everything after base_url.
please don't suggest me about using common controller, because I already know that & anyhow common controller its hide everything so its not required the encryption as I believe.
Waiting for your positive response, hopefully my problem will be solve soon. T.I.A
Well i never encrypt any URL before but you can use a php function url_encode
And "str_replace" function.
the reason for using "str_replace" beacause url_encode only encode special character in URL.
Hope I help some.
Try the code below.
urlencode(str_replace("your_domain.com/YourCOntrollerName/YourMethodName" , "SM5ah52" , yor_domain.com . "YourCOntrollerName/YourMethodName/YOuData"));
If not this. There is an library in CI Framework called Encryption.
You can get help from there Encryption.
Go with URI Routing and define one controller to decode whatever you are passing, and call proper controller / method from it.
You can use URI Routing with regular expressions.
$route["other_controllers/method"] = "other_controllers/method"; //you can add this kind of lines to not to affect other controllers
$route["([a-zA-Z0-9]+)"] = "home/decrypt/$1";
In the home controller, You can
Redirect to the page
Or
Load a view
public function decrypt($token){
//geting the page according to the token from database.
$desired_page = $this->some_model->get_page($token);
//if you want to redirect
redirect($desired_page);
//if you want to load a view
$this->load->view($desired_page);
}

Laravel How to get Url

When trying to get page url with
URL::current();
it's ignore the pagination.
For example:
current url is www.google.com/searc?page=2
dd(URL::current());
gives us www.google.com/search. Using default pagination.
My question is: Any function for get url with paginations?
Simply use :
\URL::full();
It will give the url along with query string.
You will need the full URL, not just the URL. Try this:
\URL::full();
As described in the docs of Laraval. You can use URL::full()
Accessing The Current URL
If no path is provided to the url helper, a
Illuminate\Routing\UrlGenerator instance is returned, allowing you to
access information about the current URL:
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;
echo URL::current();

Which redirection to use when redirecting URL to SEO Friendly URL?

I am developing a website using Codeigniter.
i want to redirect a URL to its SEO Friendly version. for eg.
I have URL
A. http://www.example.com/post/[post-id]/
I want this URL to redirct to SEO Friendly version of itself
B. http://www.example.com/post/[post-id]/[post-title]
Just like stackoverflow is using and like redirecting A URL to B URL.
http://stackoverflow.com/questions/[question-id]/[question-title]
I was using 302 redirection in the Codeigniter until i read somewhere that if you are using 302 redirection.
then google might treat you as a Spammer but then again when i saw Stackoverflow URL Pattern then i think its much better to have B version of URL.
So my Questions are:
1. Which redirection stackoverflow is using?
2. Is it better to Store the Slug for [post-title] in database or manually calculate it with url_title() function.
302 means a temporary redirect. The result is search engines will still index the original URL.
301 mean permanent redirect. This results in the search engines transferring index data to the new URL.
302s are not spam but if used in the wrong situation you don't helping yourself.
In your case you will be permanently moving your URLs so a 301 is appropriate.
create a private function in the posts controller for SEO friendly URLs for posts.
private function _redirect($url) { // $url = http://www.example.com/post/[post-id]/
redirect($url . $this->post_title); // becomes http://www.example.com/post/[post-id]/[post-title]
return;
}
You should have a post_title member variable set before redirection. Where ever you are calling a redirect() function, replace it with your private function _redirect() and you are good to go.
by default its 302 redirect. To do a 301 redirect just add parameter to the redirect() function. e.g
// with 301 redirect
redirect($url . $this->post_title, 'location', 301);
Note: In order for this function to work it must be used before anything is outputted to the browser since it utilizes server headers.

How do I direct to www.whatever.com using CodeIgniter's "anchor" function?

I'm using CodeIgniter (because it's awesome) and I have something like:
<?php echo anchor("/", "whatever.com" ); ?>
However, this results in http://www.whatever.com/.html which is not right. Help?
Is there any reason why you are using the anchor? It's purpose is to help you create anchors for your site, not really for external sites. If you are linking to an external site, just create a regular link?
The anchor helper parameters are
anchor(uri segments, text, attributes)
If you want to use the anchor function in CodeIgniter to link to an external site you must include the protocol part of the URL. So if you want to link to www.whatever.com you must write
anchor('http://www.whatever.com', 'The site name');
If you don't include the protocol part of the URL, CodeIgniter will think you mean an internal link and will create a link relative to the base URL of your site.
Digging into the CodeIgniter URL helper code you find
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
www.whatever.com is not matching the regular expression so you are getting an anchor with a URL relative to the site's base URL.
It has added .html to the end because you have a url_suffix in your config. As Wil says, anchor is not really meant for external sites.

CodeIgniter routing problem. (appends ajax route to existing url)

I'm trying to perform an AJAX-request in a view, the user gives some input which is sent to the server with AJAX and the function it's supposed to go to is routed with CodeIgniters routes.
This is the view I'm currently standing in while making the request.
http://localhost:8888/companies/list
In my route config I've set this route below to handle the AJAX-request, which should be able to come from any view and still be able to go to the route I've specified.
$route['test_ajax'] = "ajax/test_ajax";
So the request should go to the "ajax"-controller and use the function "test_ajax", which should make the POST-url look like this.
POST http://localhost:8888/test_ajax
But instead what I get is the current URL I'm standing at, and the route I've specified appended to the URL crashing my response from the AJAX-request completely since it didn't even go close to the function it's supposed to. The POST-url I get looks like this.
POST http://localhost:8888/companies/test_ajax
Notice how the parameter of /companies was removed. The argument /list was lost somewhere, al though if I add a trailing slash after the list I get the list argument in the URL as well.
So what just happened is that the POST tries to go to the companies-controller and look for the function test_ajax which is defined in the ajax-controller and not in the companies-controller. This error keeps occuring no matter what URL I'm at, and it always follows the same pattern. It keeps appending my route-URL to the existing URL instead of routing correctly.
So what could be causing the routing to behave this way, is there any setting that's accidently enabled or anything? Because I know I've got this to work hundreds of times in previous projects.
Thanks in advance.
It is because your Javascript is using the current directory as the base, and appending the AJAX URL to it. Because you are (to the client-side at least) in the companies directory, it appends your URL onto this.
The solution, if your Javascript is inline, is to just use the base_url() PHP function wihtin the code ...
var url = '<?= base_url(); ?>test_ajax/'
If your Javascript is not inline, you can declare a global variable at the top of your HTML document using the PHP function...
var BASE_URL = '<?= base_url(); ?>'
And use it everywhere else in your Javascript ...
var url = BASE_URL + 'test_ajax/'
Alternatively, you could just hardcode your base URL, but that could get real messy real quick.
Turns out, CodeIgniter interpreted this as a relative link due to the fact that there was no heading slash. CodeIgniter User-Guide states that no heading or trailing slashes should be written in the routes config.
What solved this though was adding a heading slash in the java-URL.
$.ajax({
url: "/test_ajax",
type: "POST",
data: data,
success: function(data){
console.log(data);
}
});
This forces CI to interpret this as a absolute URL and gives me the URL I was looking for.
POST http://localhost:8888/test_ajax

Resources