Codeigniter url redirecting from address url - php

I have a problem with codeigniter, i can't find a resolv.
How can i make a redirect by parsing into url the destinations url without interpreting the / as a separator betwen controller model etc.
For example:
http://www.example.com/http://www.google.com/?q=test
what it shoud do to thake http://www.google.com/?q=test as a string and put it into
Header("Location: http://www.google.com/?q=test");
any ideeas ?

You can use the url helper's redirect function. If you give it a full address (with http://), it will redirect to that external url.
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

redirect("controller_name/function_name");
this code is using header function in php
it is equivalent tobase_url() + redirect value

I success make that,
I used url library like has sugest Juul, and the code is like (in controller):
class url extends CI_Controller {
function index() {
# the codeigniter not accept the characters after ? and i make a custom code
# to know where is the ? sign and then i replace http:/ whith two //
$url = str_replace(":/","://",str_replace('%5E','?',$this->uri->uri_string));
#the url var will return http://www.google.com/?q=test
}
}

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.

CodeIgniter GET method and SEO friendly URLs

In my web page search input I am using the get method, and the URL is something like this www.example.com/search?city=example&service=example2, but I want a URL like this www.example.com/search/example/example2; how can I convert?
Or maybe there is some other solution to get the data from html inputs and get a friendly URL?
I think its help you and also SEO.
Please write following uri rules in : application/config/routes.php
$route['search-(:any)-(:any).html'] = 'search?city=$1&service=$2';
its rewrite your url from www.example.com/search?city=example&service=example2 to www.example.com/search-example-example2.html
In your routes.php file inside the config folder:
$route['search/(:any)'] = 'search/searchFunction'; //You will now need to have a searchFunction() inside the controller search. Or you can just have 'search' and handle the code below inside the index() function.
Now, inside your searchFunction:
function searchFunction()
{
$city = (isset($this->uri->segment(2))) ? $this->uri->segment(2) : null; //if you have www.example.com/search/example
$service = (isset($this->uri->segment(3))) ? $this->uri->segment(3) : null; //if you have www.example.com/search/example/example2
//General Pattern is: www.example.com/uri-segment-1/uri-segment-2/uri-segment-3.....and so on.
// Now when you are querying, if($city) then 'WHERE city = $city' and so on.
//Also, in the link that was earlier triggering 'www.example.com/search?city=example&service=example2'
//like <a href='www.example.com/search?city=example&service=example2'>Link</a>
//Now have it as:
// <a href='www.example.com/search/example/example2'>Link</a>
}
I see others posting about routing, yes i can do like what, but i think first i need to solve problem with html submit action, because when i press the button its auto redirect to what standart format url search?city=example&service=example2, but like i say i need other format. So how i can solve this problem?
My form action
<form method="get" action="http://example.com/search">

Pass a full url with GET parameters via GET

I'm trying to create a intermediary page, i.e user clicks on a link that leaves the site a page tells you that you're now leaving the site.
An example link would look like this:
http://example.com/transitionpage.php?r=http://www.google.com
transitionpage.php then works with a simple
$redirectto = $_GET['r'];
header( "refresh:2;url=".$redirectto );
However I'm running into the problem that if the url you're redirecting to also has multiple GET parameters in it, the domain gets cut off at the first occurrence of &
So if the link was originally:
http://example.com/transitionpage.php?r=http://www.google.com?par=1&par=2
It would become:
http://example.com/transitionpage.php?r=http://www.google.com?par=1
Which is unfavorable.
How do I pass on the full URL via GET without it getting chopped off ? Do I have to escape it ?
You can do URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp
Your get query would translate to: http://example.com/transitionpage.php?r=http%3A%2F%2Fwww.google.com%3Fpar%3D1%26par%3D2
These are the PHP methods you need: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php
Use:
$link = 'http://example.com/transitionpage.php?r='. urlencode('http://www.google.com?par=1&par=2');
Use the built in function for Encoding Urls
for example
urlencode("http://www.google.com");
for function refrence
Urlencode Php

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.

what is function redirect() in php

I use a code to connect gmail and get my friends list. In that code there is a function call
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
I've searched for function redirect() but didn't find it in the php manual. Is it a built in function in php?
The 2nd parameter is 'location' what is the use of that parameter?
Here is the function where it's used:
public function connect_google($oauth=null){
if(!$oauth)
{
return null;
}
//create a gmailcontacts objects
$getcontact = new GmailGetContacts();
$accrss_token = $getcontact->get_request_token($oauth, false, true, true);
$this->ci->session->set_userdata('oauth_token', $accrss_token['oauth_token']);
$this->ci->session->set_userdata('oauth_token_secret', $accrss_token['oauth_token_secret']);
//redirect to google auth
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
}
It is part of the CodeIgniter URL helper. See:
http://codeigniter.com/user_guide/helpers/url_helper.html
From the documentation:
Does a "header redirect" to the URI specified. If you specify the full site URL that link will be build, but for local links simply providing the URI segments to the controller you want to direct to will create the link. The function will build the URL based on your config file values.
As you've said, it isnt a built in function, so we dont know what it should look like.
However, considering the name i guess it should look like this:
function redirect($url, $header)
{
header("$header: $url");
}
Since sending a Location: {ur} header will redirect your page to another.
Use header: http://php.net/manual/en/function.header.php
header('Location: urlOfDestination');
It is probably a user defined function. It probably works hand in hand with header() hence the name, the first parameter is the page to redirect to, and the second one is to tell the function that it is indeed a redirect to the location. Just check out the header function.

Categories