%20 is added instead of spaces - codeigniter

I guess this is small issue but yet i had to ask here since i am running short in my project. When I pass string to the function in another controller, it changes spaces into %20 sign. I guess the controller thinks the string passed as url and encodes it. But I don't know exactly how to remove it or if possible do not let it to change spaces into %20. Here is the code which i use;
$message="The user name you provided is already in our database";
redirect('admin/add_user/'.$message);
Here is my controller function where i receive the message;
public function add_user($message)
{
echo $message;
}
I also tried this as;
public function add_user()
{
echo $this->uri->segment(3);
}
But the result is same. Here is the output of the string;
The%20user%20name%20you%20provided%20is%20already%20in%20our%20database

Try this:
public function add_user($message)
{
echo urldecode($message);
}
You can read more about urldecode here: http://php.net/manual/en/function.urldecode.php

Try this:
echo urldecode($message);
because you are passing the message as part of the URL (The redirect does a new http request) it is automatically url encoded. You just need to decode it once the server receives it.

When saving to the database use:
htmlentities($variable)
when outputing use:
echo html_entity_decode($variable, ENT_COMPAT, 'UTF-8');

Related

How i can get the variable data as string like a dd() function in Laravel?

I need to get the request data but i cant get ip, fullUrl and others with all() method (this only print input values), but when i use "dd(request())" this show me all data (i need the data what is printed with dd method, but like a string to save, withour the exception who print this data). Im debbuging my app so i need to save every request data in a log file, something like:
\Log::debug($request)
So,
You can use:
\Log::debug($request->toString());
or alternatively you can use
\Log::debug((string) $request);
The Laravel Request object comes from Illuminate\Http\Request which extends Symfony\Component\HttpFoundation which exposes the following code:
public function __toString()
{
try {
$content = $this->getContent();
} catch (\LogicException $e) {
return trigger_error($e, E_USER_ERROR);
}
$cookieHeader = '';
$cookies = [];
foreach ($this->cookies as $k => $v) {
$cookies[] = $k.'='.$v;
}
if (!empty($cookies)) {
$cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
}
return
sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
$this->headers.
$cookieHeader."\r\n".
$content;
}
__toString() is considered a magic method in PHP.
The __toString() method allows a class to decide how it will react
when it is treated like a string. For example, what echo $obj; will
print. This method must return a string, as otherwise a fatal
E_RECOVERABLE_ERROR level error is emitted.
You can read more about it in the official documentation.
I highly recommend to store just what you want from request data if you don't need all of them, however for both cases you can take a look at serialize and json_encode

Ask, how to use redirect without contition?

i have codes like this
function download(){
$id = $this->uri->segment(3);
$dat = $this->mikland->gidiklanfoto($id);
foreach ($dat as $item){
$name = $item->foto;
$data = file_get_contents(base_url()."/uploads/".$name); // filenya
force_download($name,$data);
}
redirect('cikland/viewiklan/'.$id);
}
when the function are running, redirect cannot run.,
somebody can help??
i think is a simple thing but i dont know the trick., thank's before
At the end of force_download() there is an exit() statement, so no code after a forced download will run.
And you are trying to have several files downloaded at the same time - using some sort of multipart mime type, that might or might not work, but not in the given case, because CI's force_download() does not seem to support that.
An alternative to that would be creating a temporary archive file which contains all the files for download; please have a look at the official documentation on compression and archives for that.
If you'd want to send a redirection header along with the file, you'd have to do it like this:
function download(){
// add this somewhere befor the download
header('Location: '.site_url('cikland/viewiklan/'.$id));
$id = $this->uri->segment(3);
$dat = $this->mikland->gidiklanfoto($id);
// only first item is downloaded
foreach ($dat as $item)
{
$name = $item->foto;
$data = file_get_contents(base_url()."/uploads/".$name); // filenya
force_download($name,$data);
}
}
But the question would remain how the browsers would deal with a redirect and content: most likely you would only get the redirect.
You need load url helper.
$this->load->helper('url');
after
redirect("cikland/viewiklan/$id", 'refresh');
or
redirect("cikland/viewiklan/$id", 'location', 301);
Font: http://ellislab.com/codeigniter%20/user-guide/helpers/url_helper.html
redirect() method redirects to a URL. You need to pass it a full URL (as it uses the header() function which according to the RFC for HTTP1.1 requires a full URL.
so you need to hard code the full url like the given example - redirect('http://www.yoursite.com/cikland/viewiklan/'.$id);

Questionmark in url

I'm trying to use this http://api.jqueryui.com/autocomplete/#option-source with Laravel and so I need to send a GET request to a url which ends with "?term=foo". I've tried to escape the "?" with a backslash, which doesn't work. To clarify, this is what I want:
Route::get('search\?term=(:any)', function()
{
//do something
}
Is it possible to have questionmarks in the url with Laravel?
Just for others who may want a Clear Answer :
you have to write and use your code as follows:
Route::get('search', function()
{
$term = Input::get('term');
if(isset($term)){
//do other stuff !
}
}
Having a question mark in the URL should make no difference. You're using a PHP framework, and simply speaking, ...?term=parameters should not be problematic. To my knowledge, there should be no need to escape such a question mark... It is handled appropriately by default.
I believe the slug function is what you are looking for: http://laravel.com/api/class-Laravel.Str.html
From the API Doc:
slug( string $title, string $separator = '-' )
Generate a URL friendly "slug" from a given string.

displaying page with value with error message in codeigniter

Hi guys I make my code simplier just to display the error message but still now showing.
public function check_profile_ifexist($id)
{
if($this->input->post('edit')){
$this->form_validation->set_rules('email','Email','trim|valid_email|is_unique[user_details.email]');
$this->form_validation->set_rules('username','Username','trim|min_length[6]|max_length[20]|is_unique[user_details.username]|xss_clean'); $this->form_validation->set_message('is_unique',"That %s already exists!");
$this->form_validation->set_message('max_length', 'the maximum characters for %s is 20');
$this->form_validation->set_message('min_length', 'the minimum characters for %s is 6');
if ($this->form_validation->run())
{
$this->load->model('model_user_manage');
$em=$this->model_user_manage->update_all($id);
}
else
{
$this->view_profile($id);
}
}
}
Instead of echoing out all of your errors just store them all to a property:
First initiate an empty property so you know it exists - you could do this at the beginning of check_profile_ifexist() method:
$this->profile_errors = '';
Then within your check_profile_ifexist() method just add the errors to the property as you go rather than echoing them e.g.
if (MD5($username)==$pass){
echo "username cannot be the same as password";
}
becomes:
if (MD5($username)==$pass){
$this->profile_errors .= "username cannot be the same as password\n";
}
This property will then be available to all your methods so you could add it to your data array in view_profile() like so:
if(isset($this->profile_errors)){
$data['errors'] = $this->profile_errors;
}else{
$data['errors'] = '';
}
and the errors are available in your view as $errors, so you could echo $errors; to print out all of the errors.
NOTE Obviously you will have to make sure that the property always exists or check for its existence to avoid errors of your own. I have also added newlines to your errors so that they will look a bit neater when printed out in bulk if more than one.
SECOND NOTE You seem to have a lot of stuff I would put in a model in this code, which I presume is a controller. You should probably keep all your db stuff in models or the MVC police will come for you.

CodeIgniter: urlencoded URL in URI segment does not work

I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:
www.domain.com/foo/urlencoded-url/
Any ideas what's wrong? Should I do this via GET instead?
UPDATE:
// URL that generates 404
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
// This is in my profile_manager controller
public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')
If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/
It seems that the %2F breaks things for apache.
Possible solutions:
preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
Set apache to AllowEncodedSlashes On
bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
double url encode it before sending
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF
You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:
http://codeigniter.com/forums/viewthread/81365/
http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
Passing URL in Codeigniter URL segment
I actually had to do urlencode(urlencode(urlencode(
and urldecode(urldecode(urldecode(
3 times!! and it finally worked, twice didn't cut it.
try
function __autoload($class){
if(!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0] = urldecode($_SERVER['REQUEST_URI']);
}
}
in config.php
this method work for me
This is very old, but I thought I'd share my solution.
Instead of accepting the parameter as a url path, accept it as a get variable:
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
and in code:
function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
$url_current = $this->input->get('url_current');
...
This seems to work.

Resources