Having an issue here where the
$_SERVER['REQUEST_URI']
is spitting out:
/dev/nava2/store/index.php?route=product/product&product_id=48
the actual URL is /dev/nava2/store/index.php?route=product/product&product_id=48
obviously, the difference being the & on the top vs. the & on the bottom
full code looks like this:
$currentpage = $_SERVER['REQUEST_URI'];
$classic = "/dev/nava2/store/index.php?route=product/product&product_id=48";
if ($currentpage == $classic)
{ $classicclass = "current";
}
else { echo "";}
Any suggestions?
& is the html entity corresponding to &. You can obtain to original string back with html_entity_decode :
$original = html_entity_decode($_SERVER['REQUEST_URI']);
You can use html_entity_decode() to get the actual url but the top one should work. I dont think you need to change anything. You could also use str_replace or preg_replace if you really need to change some parts of your uri.
echo html_entity_decode($_SERVER['REQUEST_URI']);
Related
I have some problems generating the url i want to put in my pagination numbers. I'm setting the url inside a class with
$this->url = rtrim($_SERVER['REQUEST_URI'], " /");
then inside another class I'm setting the href value on the pagination number with
echo "<a class='active' href='".$this->page->url."/".$i."/'>".$i."</a>";
So when i now navigate to my page the url is like this
localhost/designv2/blog/
Then when i click on number 1 in the pagination i get
localhost/designv2/blog/1/
But, then when i click on number 2 in the pagination i get
localhost/designv2/blog/1/2
And if i click on number 3 i get
localhost/designv2/blog/1/2/3
Why, does it keep on adding numbers to the url instead of replacing the old number?
I could split up the url, run it through a for loop and remove the last parameter but I'm using this url for other things on my pages aswell so i cant just remove the last parameter.
Any suggestions?
Before appending ID at the last in url, check and remove ID if exist.
Instead
$this->url = rtrim($_SERVER['REQUEST_URI'], " /");
Replace last occurence of /\/[0-9]\/$/ here (/1/ or /2/) to /.Try something like this
$url = $_SERVER['REQUEST_URI'];
$regex = '/\/[0-9]\/$/';
$this->url = preg_replace($regex, '/', $url);
Live demo
Try this code
echo "<a class='active' href='/designv2/blog/".$i."/'>".$i."</a>";
I'm very new to php and can't find answer to my issue.
I try to echo a string with special characters (ie. à,é) from the URL.
URL string:
preview.php?content=<p>blà é bla</p>
Expected echoed result: blà é bla
So I do this:
$cont = $_GET['content'];
echo $cont;
Result:
bl
So, even if my page has a <meta charset="UTF-8">, I tried:
$cont = $_GET['content'];
$cont = html_entity_decode($cont, ENT_COMPAT, 'UTF-8');
echo $cont;
Same result:
bl
I tried with a header (header('Content-Type: text/html; charset=UTF-8');) at top of the page with same result.
What's weird is that is I try this:
echo html_entity_decode("<p>blà é bla</p>");
or even this:
echo "<p>blà é bla</p>";
I get the expected result:
blà é bla
So, I don't think it's a charset issue but can't understand why it works with the literal string but not with the get variable, can someone help me?
Encode your URL with: urlencode and htmlentities for example:
$url = 'content=' . urlencode('content=<p>blà é bla</p>');
echo '<a href="preview.php?' . htmlentities($url) . '">';
#JustOnUnderMillions, is correct.
E.g:
If you try to access data from URL like
http://localhost/index.php?content=%3Cp%3Eblà%20é%20bla%3C/p%3E
echo $_GET['content'];
OUTPUT: bl
But when you pass data by encoding data like
http://localhost/index.php?content=%3Cp%3Ebl%26agrave%3B%20%26eacute%3B%20bla%3C%2Fp%3E
echo $_GET['content'];
OUTPUT: blà é bla
It is better to use urlecode function for encode data in URL.
Thanks all.
This works perfectly !
Actualy, as my URL is generated with javascript, I did a encodeURIComponent on my link's href variable and it works as well.
If you want to pass a URL with parameters as a value in a URL and through a javascript function, such as.
Pass the URL value through the PHP urlencode() function twice, like this
<?php
$url = "index.php?id=4&pg=2";
$url = urlencode(urlencode($url));
echo "<a href=\"javascript:openWin('page.php?url=$url');\">";
?>
On the page being opened by the javascript function (page.php), you only need to urldecode() once, because when javascript 'touches' the URL that passes through it, it decodes the URL once itself. So, just decode it once more in your PHP script to fully undo the double-encoding.
<?php
$url = urldecode($_GET['url']);
?>
If you don't do this, you'll find that the result URL value in the target script is missing all the var=values following ? question mark.
index.php?id=4
kindly check this online tool also : URL Encode , URL Decode
Still learning php as I go so this might just be something I haven't gotten to yet but it's the next roadblock in building my personal site. I have a basic understanding of includes such as linking:
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
to pull my includes but I've come to a small problem in that my generic art-gallery page needs to switch between a 'portfolio' header and an 'artwork' header. So I figured I could either build "art-gallery.php" AND "port-gallery.php" and go back and relink everything or just make it so that when you call the link like the above code I just specify which header goes with it. Unfortunately this would also require going back and changing every link. But I noticed that I did state:
...&menu=side-menu-portfolio...
and the pages are already calling side-menu-artwork or side-menu-portfolio so if I could just call in menu and cast aside the 'side-menu-" portion then it would just use artwork or portfolio and call the right header. Unfortunately this is where my limited knowledge of php and syntax come in. I have tried to produce the following code based on my php and js understanding:
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
but I don't know if my syntax is just wrong or if what I'm trying to do is impossible to begin with. Note that when I try this I get
Function Include error of "Warning: include(headlines/headline-.php)"
so it looks like everything else is reading correctly, I just don't know if or how I can extract the word I want from the rest of the menu name.
Should be, Assumed your included file name is headline-side-menu-portfolio.php
<?php
$filename = str_replace("side-menu-", "", $_GET['menu']); // headline-portfolio
include("headlines/headline-" . $filename . ".php"); // headline-portfolio.php
?>
Something like this :
<?php include("headlines/headline-" . $_GET["menu"].".php"); ?>
<!--gives include("headlines/headline-side-menu-portfolio.php")-->
where
$_GET["menu"] = 'side-menu-portfolio'
Try this:
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
Your code is wrong.
Instead of
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
try
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
You should check if the file exists before you try including it.
if (file_exists($filesrc)) { ... }
Better yet don't let the user change the menu through a $_GET variable. Instead link to a specific page or pass a variable then decide what menu to get. Like
switch ($_GET['menu']) {
case 'side-menu':
include("headlines/headline-side-menu.php");
break;
}
Just use
$_GET['menu']
, the "side-menu-" part is already in the content of your variable passed as param.
You propably want to do an if .... else so to include one header or another based on the $_GET variable menu.
So something like this will do this:
if($_GET['menu'] == 'side-menu-portfolio') {
include 'headliens/side-menu-portfolio.php';
} elseif($_GET['menu'] == 'side-menu-other') {
include 'headliens/side-menu-other.php';
}
okay....your are almost there....just quotes missing from include syntax...it should be
include("headlines/headline-.php"); /* notice the quotes*/
so it should be
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
where $_GET['menu'] should be in the url, like:
art.php?id=image id&name=This-is-my-title&menu=side-menu-portfolio
so what's happening her ??
Upon execution of the line :
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
$_GET is fetched from the url and replaced in the header tag, so now the header tag becomes :
<?php include("headlines/headline-"."side-menu-portfolio".".php"); ?> => <?php include("headlines/headline-side-menu-portfolio.php"); ?>
Also. may i suggest that for :
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
don't use space in the url, either replace it by - or _
I have a database table that stores URL.What I need is grab those URL's from table and make it click-able with the URL's title as anchor.
This is what I have tried:
while($row4 = mysql_fetch_assoc($result4))
{
echo "".$row4['Title1']. "";
}
It displays for example my tilte1 that is youtube and Url1 is www.youtube.com.
But when I click on it it is going to localhost/mysite/www.youtube.com
How can I fix this?
try:
echo "".$row4['Title1']. "";
Add http:// in front of the link. Then it will go to where you wanted.
you need http:// in front.
echo ''.$row4['Title1']. '';
Can you check if your Url1 field is a proper url? see if it has http:// protocol in the url. if not you will need to add it to prepend it to your table or programmatically prepend http:// protocol to your link.
Additionally you can use below function taken form codeigniter framework. It prepares your link for url. do prep_url($row4[Url1]) instead of just $row4[Url1];
function prep_url($str = '')
{
if ($str == 'http://' OR $str == '')
{
return '';
}
$url = parse_url($str);
if ( ! $url OR ! isset($url['scheme']))
{
$str = 'http://'.$str;
}
return $str;
}
Try with this
while($row4 = mysql_fetch_assoc($result4))
{
echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>";
}
You should make an absolute link from that, and don't forget to put attributes' values in quotes.
I suggest this:
echo ''.$row4['Title1']. '';
//by doing this you also won't need any of \ slashes
I enter urls enclosed in quotes, example:
"http://google.com"
Then I use:
.$row['date']."< a href=".$row['title'].">".$row['title']."< /a>".
the result is a clickable link in the form of:
http://google.com
remove the space between < and a, ( i had to add a space for the code to post.
For example I have a website that points to a page like this:
http://www.mysite.com/folder/file
How can I get determine /folder so that I can go further an quote an if statement like
if /folder then echo something
Why do I need this?
I am trying to tell facebook which image to pick from a page. Actually I have a pretty simple page structure and the image that facebook should take is always at first but somehow it does choose another one from time to time. I guess because the other images are loaded faster. And the old way to rel="img_src" doesn't seem to work anymore as that I could just add it to the wanted image.
So well of course I use the open graph protocol to tell facebook which Image it should use.
I am working with a cms were I can output the path of the image depending on the id the image has. I have two different id's for the different kind of pages living in two different folders.
This leads to:
if index --> echo meta og for index img
else if /folderone (with id1) --> echo meta og for id1
else if /foldertwo (with id2) --> echo meta og for id2
This is why I need to know the foldername.
Now with the answer I have following setup, just that you know:
<?php $folder = dirname($_SERVER['SCRIPT_NAME']); ?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/") echo "<meta property='og:image' content='http://www.mysite.com/img/img.jpg'/>" ;?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderOne") echo "<meta property='og:image' content='http://www.mysite.com/img/{$img_id1}'/> " ;?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderTwo") echo "<meta property='og:image' content='http://www.mysite.com/img/{$img_id2}'/> " ;?>
parse_url &
explode
$path = parse_url($url, PHP_URL_PATH);
gives you
/folder/file
then you can explode() to separate the path values and check the first one to see if it is 'folder'
Example here: http://tehplayground.com/#7TIKAwp6J
Example code:
$url = "http://www.mysite.com/folder/file";
$path = parse_url($url, PHP_URL_PATH);
$arr = explode("/",$path);
echo $arr[1]; // leading slash makes [0] ""
outputs
folder
$script = $_SERVER['SCRIPT_NAME'];
echo dirname($script);
Possibly use "get current working directory" function getcwd()?
Explode it by directory separator.
Then grab the last element like this:
$var = getcwd();
$var = explode('\\', $var); // your OS might use '/' instead
$var = end($var);
I suppose this assumes you're not using some kind of MVC framework that uses routing.
I hope that helps!
I think this is nicer than exploding the string:
function getCurrentDirectory(){
$curDirPath = getcwd();
return substr($curDirPath, strrpos($curDirPath, '/') + 1);
}
getcwd() gives you the current directory's path, and then you can truncate it starting right after the last occurrence of the / in its file path.
$dir_list = explode('/', dirname($_SERVER['SCRIPT_FILENAME']));
$this_folder = $dir_list[count($dir_list)-1];
...
if ($this_folder) == "folderOne") echo "...."
...
if(dirname('yoursite/folder')){