How do I echo forward slash in php - php

I want to echo forward slash in php as follows
<a class="submenu" href="<?php echo base_url('products').'/'.rawurlencode('Agarbatte/Candle');?>"
Agarbatte/Candle is not a directory but when i do this, href takes this as directory and give me the error page not found.
Any help will be appreciated.

A slash in the URL will always be treated as a directory separator. And if you replace it with something else you won't have a slash anymore. But that's most likely the easiest solution..
If you have a 1:1 mapping between the path in the URL and your filesystem you are out of luck. If your application uses a "routing layer" though, you can modify it to not treat the / as a separator when some criterium is met.

Just use 'urlencode' on the 'Agarbatte/Candle'. I do not see a special need for 'rawurlencode'.

Though you can use the urlencode() function as others have said, I recommend not getting into this habit. Assuming you have these products in a database, use the id and you'll never run into these issues.
For example if the Candle product has id of 6 in the database... the PHP should resolve to: <a class="submenu" href='products/6'>
Furthermore, there are some shortcuts you can take here which will help you in the long run.
Instead of <?php echo "something" ?> turn on php short tags in php.ini and use <?= "something" ?>
You don't need to call base_url every time you write a link. Use the <base> HTML tag appropriately: <base href="http://localhost/yourapp/"> in <head>
So... with that said after using 1 and 2:
<a href='products/<?= $id;?>'><?= $name_of_product ?></a>

how about using "Agarbatte-Candle" in the url then on the output page
$url_bit=string_replace('-','/',$url_bit);

Related

How to use unicode in CodeIgniter?

I am using CI framework for my project. If I use like this
$title= "प्रदेश"; // प्रदेश is written in nepali langauge.
echo $title; // It will display प्रदेश
Now I want to use
<a href = "<?php echo base_url("home/$title")?>"> //home is a controller
Now In home controller if I tried to display$title it will not show प्रदेश.
It will display like this %E0%A4%AA%E0%A5%8D%E0%A4%B0%E0%A4%A6%E0%A5%87%E0%A4%B6
Please help me.
The base_url calls urlencode on the string, which makes it into url entities.
A space becomes %20 and things like that.
The url that is displayed is actually correct and should be interpretted correctly by the server, even if it's gibberish according to humans.
If you really want the human readable characters for some reason you can do two approaches, urldecode the resulting url(not recommended):
echo urldecode(base_url("home/$title"));
or
echo base_url("home/").$title

Cannot make a href link to work

I am trying to include pagination on a site and I encountered this problem.
I have the following line, but instead of index.php I must put index.php?view_vehicles.
This is the line:
echo "<a href='index.php?page=$next'>Next</a> ";
It's the first time I encounter this issue and I don't know how to make it work.
From comments:
"It must be something like this: <a href='index.php?view_vehicles?page=$next'>Next</a> but it's now working this way. – AndreiCT
As I stated, additional GET parameters require & after the first parameter, not 2x ?.
<a href='index.php?view_vehicles&page=$next'>Next</a>
You should concatenate the variable $next in the string you're echoing:
echo "<a href='index.php?page=".$next."'>Next</>";

how to display a picture from a folder php

I have a folder where uplaoded files are saved. This page is used to retrieve those saved files. A picture called "flower.jpeg" is in the folder, and I am trying to display the picture using an image tag, as shown below. I have two options, but none of them is working.
<!DOCTYPE html>
<html>
<body>
<?php
$picture=flower.jpeg
$playfile='/upload/$picture';
?>
// <img href= '<?php $playfile ?>' width="800" height="600">
// <img src= '<?php echo $playfile; ?>' width="800" height="600"> <br>
<script>
document.write('Go Back To Chat');
</script>
</body>
</html>
flower.jpeg requires quotes and the $playfile assignment is not properly specified as, to my knowledge, strings within single quotes are not parsed for variables within PHP.
$picture='flower.jpeg';
$playfile='/upload/'.$picture;
Additionally correct use of the tag is as follows
<img src="<?php echo $playfile; ?>" width="800" height="600">
As the attributes need enclosing by double-quotes, you are just using PHP to echo out a variable containing a string.
An explanation of what is wrong. You're using the following code to build your link:
$picture=flower.jpeg
$playfile='/upload/$picture';
First off, you need to quote the picture
$picture = 'flower.jpg';
//or
$picture = "flower.jpg";
The next problem is how you're building the playfile. You need to exit single quotes to use a variable or use double quotes:
$playfile = '/upload/'.$picture;
//or
$playfile = "/upload/$picture";
//or even
$playfile = "/upload/".$picture;
The single quotes will take the text as is where the double quotes will evaluate variables in them. I suggest continuing to read up on the basics of PHP and strings.
Here are a few steps that may help you in finding if the path to the image (or the 'href' as you call it) is correct. No direct solution is possible as we do not have access to your file system, as Simon pointed out.
Check if the file extension of your picture is mentioned correctly. Check also that it is in the correct CAPS. For example, if you saved your picture using MS Paint, the extension will be *.PNG but your script may contain *.png; this may cause problems in some cases.
.jpg and .jpeg are two different extensions sometimes (in the sense of the above point). You may want to take that into consideration.
You may want to verify that the directory structure of the image is correct (i.e. verify that you are specifying the correct path to the file).
Hope this helped you in some way. The above steps are rough and vague. If they failed to help you, please comment, and someone will be able to help you.

PHP: How to decode eval()?

I just noticed today that I have got lots of spam links in my wordpress blog. I just found a file which contains
<?php eval (chr(101).chr(114)...
Its very very long string. Can someone tell me how can I decode this to see what it does? So that I can try to remove the spam links?
Thanks.
Just replace eval by echo and have a look at the generated output
<?php echo (chr(101).chr(114)...
Instead of executing (eval) you can just echo out what it says, preferrably with htmlspecialchars if you execute it via browser:
<?php echo htmlspecialchars(chr(101)...
odds are though that you won't see anything understandable, since it is probably encoded in more ways than one.
Simply replace eval with echo:
<?php echo (chr(101).chr(114)...
Besides that, you most likely need to reinstall whatever you have on your webspace as you obviously have been hacked. Ensure that you use the most recent version of Wordpress and all other software you are running to prevent this from happening again.

extra mystery space in php code

I am trying to pass a variable frm php to a javascript function, however a space keeps getting appeneded, and I can not see how.
The relevant php code snippet:
<p>Click for full description </p>".$brand."
<p>DELETE
$brand is what I want to pass, and deleteRec is the name of the function.
At the end of the first line I am echoing out brand before the link to deleteRec, and it contains no space. In my test case, it is set to simply 'o'.
The link that is genereated for deleteRec however, clearly contains a space, and I don't know where it is coming from.
DELETE
Do var_dump($brand) and look closely - there's almost certainly a space in it!
In which case, you can guard against it with trim
$brand=trim($brand);
Change:
<p>DELETE
to:
<p>DELETE
and tell us how it goes.
Try do echo the following:
echo "--$brand--";
This way you'll be able to see if there are any spaces in the variable.
As a general matter of style, I would change second link from:
<a href=\"#\" onclick=\"
deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
to:
<?php
$deleteRecArgs = "'$ARTICLE_NO', '$brand', '$pg', '$nextArticleNo'";
?>
DELETE
It's easier to read and maintain.

Categories