php get the php name from the url - php

I have these types of urls
something1/anotherThing1/View/fileName.php
something2/anotherThing2/View/fileName2.php?variable1=value1&variable2=value2
...
I want to get the fileName without the .php
I tried this
$_SERVER['REQUEST_URI']
but it seems it gives me the whole url.
Maybe I need a regualr expression.
Could you help me please. i am not good in PHP and i just need this small issue.
appreciate it

echo basename($_SERVER["SCRIPT_FILENAME"]); //with php
echo '<br>';
echo basename($_SERVER['REQUEST_URI'], ".php");//without php
check this answer
Get the current script file name

Related

why $_GET value is different than URL

I have a url like this
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
I need to get value of test. When I echo $_GET['test'] it returns 685286640293e700bc9440cafb587290?url=component/mycom/ . It is happening for last variable of url.I dont know why is this so?
Thanks
Test isn't being sent in the $_GET that you displayed?
You have : insid=7 and token=685286640293e700bc9440cafb587290 with a malformed & between them that is the HTML code for & rather than the & itself.
Your URL at the moment is this:
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
If you change it to
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
I would be willing to bet you will get your code working :)

Paste terms in URL

I am trying to find a way to create a simple dynamic URL, that gets its information from boxes where people enter something.
I got a google search machine and want to refer to it, so basically I wanted two boxes:
One for choosing which directory to search in (the google machine has different directories in its index I want people to be able to choose from those)
and the other for the search term they are looking for.
The URL looks like that:
http://searchengine.xx/search?q=SEARCHTERM&site=DIRECTORY&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8
I tried it with PHP like that:
<?php
$directory = $_GET['searchterm'];
echo "http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen& entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>
?>
This doesnt seem to work well and I wondered if this was possible in any other way (simple HTML, JavaScript maybe?)
try to mix php and htaccess...
<?php
//get the text form the text box and put it in a variable eg.($text)
$url = 'index.php?searchterms=$text';
header("Location: $url");
?>
I think that something like this might work.
Get the text from the text box, then put the text onto a variable (i've used $text to exemplify).
Put the url that you want in a variable (i've used $url to exemplify), but in the end of the url put the text variable the way i did it.
Finally, use the header function to redirect to the url that you want.
Hope i helped
There are several problems with the PHP code in your question. $searchterm is never set and the echo statement is never ended. Try this instead:
<?php
$searchterm = $_GET['searchterm'];
$searchterm = strip_tags($searchterm);
echo "<a href='http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>";
?>
The strip_tags will ensure " and ' are removed so it doesn't break your link.

Attaching a number to the end of variable php

Probably another basic question but its been annoying me for a while now...
I have a php file which is included on a php web page to bring in dynamic content from my mysql database.
Everything works fine with this except when i try get pictures to work and here is the problem.
I am using the code:
echo "<img src=fishery_images/$region/$url/$url1.jpg'/>";
All of which has been selected from the correct table and so on. an example of what i want this to resolve to is below:
fishery_images/fife/goldenloch/goldenloch1.jpg
However because i have the code:
$url1.jpg
and $url1 is not defined as anything then it resolves as the following:
fishery_images/fife/goldenloch/.jpg
i can have anything from goldenloch1.jpg all the way to goldenloch10.jpg so need to be able to say which image should be used.
how is it i say $url1.jpg without meaning $url1?
Really hope this makes sense... and thanks in advance
echo "<img src=fishery_images/{$region}/{$url}/{$url}1.jpg'/>";
http://en.wikipedia.org/wiki/String_interpolation#PHP
You'll want to use curly braces to wrap your variable:
echo "<img src=fishery_images/$region/$url/{$url}1.jpg'/>";

How to write response to file using php

I wish to write the response of hitting a given url into the href attribute of an anchor tag using PHP. How can I do this?
Here's an example of what I excpect to happen
mylink.com/getdoc?name=documentA
returns a string as a response:
mylink.com/document2012-03-15.pdf
I need to write this response (using PHP into the href attribute as shown below:
Open Document A
(so the above will be the final source of my page.
I think there are a few ways to do what you want. Not all of them will work exactly as you ask for, but the end result should be the same.
Solution one
My first possible solution was already posted by #shanethehat. You could use file_get_contents to call your PHP script via HTTP and get the response.
Solution two
Another possible solution was suggested in the comments of the post by #YourCommonSense. You could simply include the getdoc script in the PHP script that is generating your HTML file, like this:
$_GET["name"] = "documentA";
echo " Open Document A ";
Solution three
Or you could change the way the getdoc script works. You could use a script more like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment; filename=\"{$_GET["name"]}\"");
readfile($_GET["name"]);
And you keep your link like this: Open Document A . When getdoc.php is called, it will get the specified file and start a file download.
NOTE: you should probably do some input sanitization with this method (removing slashes, making sure the file ends in .pdf, etc) to make sure someone doesn't try to get a file they're not allowed to get.
That's all I'm coming up with at the moment. There might be a more clever way to do it, but hopefully one of these solutions will do it for you. I would try solution 2 or 3 first, and if they don't work out for you, then go with solution 1.
<?php
//get output from URL
$myfile = file_get_contents('http://mylink.com/getdoc?name=documentA');
?>
Open Document A
How to write response to file using php
Noway.
PHP do not process HTTP requests.
You have to set up your web server to do the rewrite.
There are 100500 questions under mod_rewrite tag, you will find the solution easily.
Note that you may wish to rewrite your url to /getdoc.php?name=document2012-03-15.pdf, not one you mentioned in your question

How to pass part of url in new link? Using only HTML & PHP

I have been trying to attempt to use the facebook share function in my website but i cant seems to have the right result.
Say:
i have a page called http://www.example.com/product.php?prod=lpd026n&cat=43
and i am using facebook's share function to have visitors to share the page in the FB wall.
i tried writing the link this way but i doesn't seems to be successful:
href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print urlencode(#$_SERVER['QUERY_STRING']!=''?'?'.$_SERVER['QUERY_STRING']:'')?>"
as the result the arguments in the URL came out to be in %26, %3D and etc..
Ie: example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
as some of you may know that the data after '?' is dynamic and i am planing to use the code above in the frame of the page, so it will have different query passed to the share link in every new item.
The end result that i want got to look like this:
http://www.facebook.com/sharer.php?u=http://www.example.com/proddetail.php?prod=lpd026n&cat=43
Not
http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
can anyone help me to solve this problem?
Thanks in advance!
Ps: if you are unclear, please ask me to further clarify.
This URL:
http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
is only partially-encoded. You actually need to fully URL-encode it before passing to FB, so that it won't interfere with FB's URL structure. I'm sure that their script will know how to parse it properly.
The correct method is:
$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');
// evaluates to:
// http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.example.com%2Fproddetail.php%3Fprod%3Dlpd026n%26cat%3D43
Update: build your dynamic query
// Original URL
$url = 'http://www.example.com/proddetail.php';
if ($_SERVER['QUERY_STRING'])
$url .= '?'.$_SERVER['QUERY_STRING'];
// Final URL for FB
$fb_url = 'http://www.facebook.com/share.php?u='.urlencode($url);
This is what urlencode does, what is the problem with the link this way?
Edit: I do not use PHP, but I think the following will do the trick (omitted the urlencode):
href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print $_SERVER['QUERY_STRING']?>"
I guess K Prime is right.
u need to encode the whole url because the slashes and ":" are still causing problems in this link ;)
$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');
should be fine for your purposes.

Categories