How to display correct link in CI anchor - codeigniter

I have a form with upload file option.
I get the file_path on $data uploaded by user. Now I want to use this
file_path to be the link in my anchor tag.
My problem is i can't get the correct link. it seems like that the base URL was being included in the link showed in the lower left of the screen.
I also read some post here to add "http://" before the link. but the link appeared was like this
c/xampp/htdocs/.../uploads/products/filename.pdf
no colon.
any idea on how can I access my uploaded file be access via link.
Thanks

paste this line in your /applications/config/config.php just right before
$config['base_url'] = "";
and change this to
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://');
$root = $protocol.$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
This approach makes your base_url() function returns the current host or url you are in. Even if you upload this to any server it will always return the exact url of your website.
Then you use it like this
<?= base_url('uploads/products/filename.pdf'); ?>
That's all you have to do. Hope this helps!

Related

I want pagination url with out href url link in codeiginter

I have use codeiginter pagination functionality. But I want pagination url with out href url link in codeiginter. how can possible please help?
you can play with Pagination library , create your own library like so
application/libraries/Pagination.php
and copy the content of CI native pagination library file into the file that you just created from
system/libraries/Pagination.php
so this way you don't touch core files
then search for "here we go..." in my version it is on line 225
underneath you can see how those links are rendered and you can change them as you wish
// And here we go...
$output = '';
// Render the "First" link
if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
{
$first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
.... etc
you can read more about Replacing and extending Native Libraries with Your Versions here http://www.codeigniter.com/userguide2/general/creating_libraries.htmll
hope that helps!

Add parameter to Codeigniter URL

I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1
Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>
You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");

How to redirect WORDPRESS hotlinked image ( for exp : Image.jpg) to post/page where it is?

Recently google change image search and as a result most of the traffic click over view original image and it send them to direct image file.
I have changed my .htaccess and stop all hot linking and redirect to my homepage if they click over image file.
Recently I've also tried if it is possible to redirect to post or page where image is.
By the following code
<?php
require('../wp-blog-header.php');
$imageURL = strip_tags( $_GET['id'] );
if imageURL!== "") {
$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment'AND post_parent > '0' AND guid = '$imageURL'";
$linkedImage = $wpdb-get_row($query);
$attachmentUrl = get_attachment_link($linkedImage->ID);
if($attachmentUrl !== "" && !is_numeric(stripos($attachmentUrl, "attachment_id=0"))) {
header("HTTP/1.1 302 Found");
header("Location: ". $attachmentUrl);
exit;
}
}
$newUrl = get_site_url() . "/image-not-found";
header("HTTP/1.0 404 Not Found");
header("Location: " . $newUrl);
exit;
?>
But it redirect to http://www.mydomain.com/?attachment_id=
id is nil or can't fetch the proper id
My website in Wordpress CMS version 3.5.1
can anyone help how to redirect to proper attachment page for .jpg file direct request.
Thanks in advance
Your code would only work if there was an ID parameter on the requested URL that would be an exact match with the image URL, for instance http://www.example.com/?id=http://www.example.com/uploads/Some-Image.jpg
Is this the case? Even so, you shouldn't use id since it's a reserved parameter for other things in WP. In your .htaccess, you should redirect http://www.example.com/uploads/Some-Image.jpg to, for example, http://www.example.com/?image_redirect=http://www.example.com/uploads/Some-Image.jpg. Then use $imageURL = $_GET['image_redirect'].
Other notes: strip_tags doesn't make a string safe to use as a SQL query. In WordPress, you have esc_sql($string) for that. Also, you should rather use if ( $imageUrl != '' ), not with !==. And there's an error on line 5, it should say $wpdb->get_row, you currently have a minus sign. I assume these are typos. And finally, you shouldn't use Location: header with a status code 404.

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

CodeIgniter -> Get current URL relative to base url

Tried URI::uri_string() but can't get it to work with the base_url.
URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi
but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.
I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?
// For current url
echo base_url(uri_string());
If url helper is loaded, use
current_url();
will be better
Try to use "uri" segments like:
$this->uri->segment(5); //To get 'ahahaha'
$this->uri->segment(6); //To get 'hihihi
form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.
Every segment function counts starts form localhost as '1' and symultaneous segments
For the parameter or without parameter URLs Use this :
Method 1:
$currentURL = current_url(); //for simple URL
$params = $_SERVER['QUERY_STRING']; //for parameters
$fullURL = $currentURL . '?' . $params; //full URL with parameter
Method 2:
$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Method 3:
base_url(uri_string());
I see that this post is old. But in version CI v3 here is the answer:
echo $this->uri->uri_string();
Thanks
//if you want to get parameter from url use:
parse_str($_SERVER['QUERY_STRING'], $_GET);
//then you can use:
if(isset($_GET["par"])){
echo $_GET["par"];
}
//if you want to get current page url use:
$current_url = current_url();
Running Latest Code Igniter 3.10
$this->load->helper('uri'); // or you can autoload it in config
print base_url($this->uri->uri_string());
I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters.
With this, and the controller you are in, you know how the URL looks like.
In you above URL this would mean you would be in the controller dropbox and the array would be something like this:
array("derrek" => "shopredux", "ahahaha" => "hihihi");
With this you should be able to make such a function on your own.
In CI v3, you can try:
function partial_uri($start = 0) {
return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}
This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:
partial_uri(3); # returns "ahahaha/hihihi"
you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string.
I found solution of this problem.
base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");
and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");
<?php $currentMenu = $this->uri->segment(2); ?>
<ul>
<li class="nav-item <?= ($currentMenu == 'dashboard') ? 'active' : '' ?>">
<a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data-
feather="pie-chart"></i> Dashboard</a>
</li>
</ul
this is work for me

Resources