using $_GET without using '?' - php

I want to create a link which is catched by $_GET in PHP. Let me explain:
Currently i do this in HTML:
Contact Us
And this is what i do in php:
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>
In this case the address becomes like this www.domain.com/?contactus.
But I want the address like this: www.domain.com/contactus
If i change html to Contact Us
but it doesn't work. Anyone please tell me. Thanks

you need a .htaccess file to make it work
like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^contactus\/?/?$ index.php?contactus
</IfModule>
and index.php like this
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>

You can use domain.com/contactus and parse the URL
$url = "http://domain.com/contactus";
$url = parse_url($url);
$path = $url['path']; // contactus
echo $path; // contactus
You can then use it
if ($path == "contactus"){
include "contactus.php";
}
You will still need URL rewrite, try adding or editing your .htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^contactus/?$ contactus-page.php [NC,L] # Handle requests for "contactus"

hi you need to do like this
at page where u set anchor tag do following
$url="contact.php?page"='contactus';
give href="$url" .
And other side you can use $urlget = $_GET['page'];
plz note the attribute u used is page so when u try to get in nother page u need to use $_GET['page'] and you can see url when you click on contact us page in your address bar there is also contact.php?page='contactus' showing

Related

url Change in core php

I am use core php and i want to change url.
This is my actual URL.
http://www.example.com/blogs/blog_single.php?title=Need-of-a-Professional
and i want that type of url:
http://www.example.com/blogs/Need-of-a-Professional
so what i do?
Please help me, How i can achieve this?
Add the following to your .htaccess:
RewriteEngine On
RewriteRule ^blogs/([^/]*)\.html$ /blogs/blog_single.php?title=$1 [L]
You can use online url rewrite tools to generate custom urls such as this one.
This is a get request and title is send in it.
To change it you have to change the method of form.
There will be other changes too as by doing this your previous code will not work properly.
What I have done is on the basis of url path(condition) i have called the controller(means the page which i have to call in result):
Like in this code line on index.php $ctrlpath is the path upto the controller then $page is the page which i have to call one the basis of condition :-
$url_parts = parse_url(preg_replace('/\/{2,}/', '/', $_SERVER['REQUEST_URI']));
$url_path = $url_parts['path'];
/*This I have done in router.php*/
if($url_path == '/saving-calc/'){
$page = 'tools/saving-calc';
}
require_once($ctrlpath.$page.'.php');
Then on your own controller you an append the title of your page in the url.
You try this..
$url = explode('/', $_SERVER['REQUEST_URI']);
//breaking the url and storing it as array based on '/'
array_pop($url);
// last part of the url is removed from array in you case 'blog_single.php?title=Need-of-a-Professional' removed from array
array_push($url,'Need-of-a-Professional');
// pushed at last index
echo implode('/', $url); //converting array into url(string)
modify your .htaccess file with below.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(\d+)*$ ./blog_single.php?title=$1
You can generate htaccess by putting the dynamic url.
generate from here.
https://www.webconfs.com/web-tools/url-rewriting-tool/
Yes,You will change easily URL manually.
Example:
Click Me

htaccess get code and redirect with php

Ex:
If we entered these url (Like This site)
http://stackoverflow.com/questions/10139779/ or http://stackoverflow.com/questions/10139779
Its automatically redirect to this url http://stackoverflow.com/questions/10139779/handling-get-with-htaccess
I want to get this code (10139779) from url and redirect to full url. Someone go to full url how to get this code (10139779)
Edit :
I created htaccess file like this.
<Files .htaccess>
order allow,deny
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(\w+)$ ./load.php?code=$1
Now i want to give video title to url after video id Like this www.example.com/16056/video-title
load.php
<?php
echo $_GET['code'];
//I can get video title from php then how to give to url it?
?>
I can get code this from www.example.com/16566 but i can't get code this from this url www.example.com/16566/video-title
after id / link was doesnt work. how to fixed it
This answer is an additional update of #bjb568's answer, which handles exceptions.
<?php
if(isset($_GET['code'])) {
$code = (int)$_GET['code'];
$url = '/videos/' . $code . '/' .getTheVideoTitle($code);
} else {
$url = '404_page_not_found.php'; // or redirect visitors to homepage
}
header("Location: $url");
exit;
?>
Also, this prevents someone intentionally "hack" the link by entering strings with escape character & crash the code. (Of course, it's developer's duty to validate the input in the getTheVideoTitle() function)
Send out a location header for the correct URL. (load.php:)
<?php
header('Location: /videos/'.$_GET['code'].'/'.getTheVideoTitle($_GET['code']));
?>
There is nothing you can do in .htaccess to get the video title, so php must do it.
Also, for .htaccess:
RewriteRule ^(\w+)$ ./load.php?code=$1
Should be:
RewriteRule ^(\w+)/*$ ./load.php?code=$1
This makes it match slash(s) after the URL. If you also want it to match the title after the URL, make it:
RewriteRule ^(\w+) ./load.php?code=$1

Redirect Script PHP

Well, I want to have a redirect script on my server.
I want it to take the text after the URL, and redirect it to a website.
The one I have right now looks like this:
<?php
$url = $_GET['url'];
header("Location: http://youtube.com/watch?v=".$url);
?>
That makes the URL looks like : http://mywebsite.com/?url=YOUTUBE CODE HERE
But, I don't want it to look like that, I want the URL to look like this : http://mywebsite.com/YOUTUBE CODE HERE
and that will redirect the user to the YouTube video.
Thanks
to achieve that, you need to use .htaccess and mod_rewrite on apache, or a similar technology.
basic examle:
/.htaccess:
RewriteEngine on
RewriteRule ^v/(.*)$ v/index.php?url=$1 [L]
/v/index.php:
<?php
$url = $_GET['url'];
header("Location: http://youtube.com/watch?v=" . $url);
?>
Look into URL rewriting. Here's a tutorial on how to do it on Apache servers:
http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/
If you don't want to use mod_rewrite for such a simple task much quicker solution is
$url = $_SERVER['REQUEST_URI'];
Just trim the URL from things you don't need.
With PHP itself its possible in this way:
http://www.mywebsite.com/your-php-file.php?{YOUTUBECODE}
<?php
foreach ($_GET as $tmp=>$null) {
$redirect = $tmp;
break;
}
header("Location: http://www.youtube.com/watch?v=$redirect");

how to change /1.php to index.php?id=1

if (file_exists($filename)) {
echo "<a class='next' href='$filename'><img src='images/next.png'</a>";
}
else {
echo "<img src='images/next-dis.png'/>";
}
well i am trying to do this I will have hudereds of pages like 1.php 2.php 3.php.... but i want them to look like this "index.php?id=1" instead of /1.php, /2.php and want to block users who try to access (for eg.) 2.php directly.. can anyone help?.. is there any simple way to do this?
The following rewrite rule in an Apache .htaccess file should do the trick.
RewriteEngine On
RewriteRule (\d+)\.php /index.php?id=$1
This basically matches any URL in the format [0-9]+.php and redirects it to /index.php?id=### where ### matches ###.php.
Search for .htaccess rewrite rules. You will find examples on similar scenarios. See the practical solutions section in - http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Inside your index.php you could do this
include($_GET["id"]".php");
This will load whatever id is the get var, however anyone could change this variable and gain access to whatever they enter here.

Get string from url example: domain.com/THESTRING

Please some one explain to me how it works and how to do it.
PHP has a function just for this: parse_url().
$parts = parse_url('domain.com/THESTRING');
$path = $parts['path'];
That wil get the part that you want
Could also use below if you want the current URI:
$_SERVER["REQUEST_URI"]
Yet another way:
echo strstr(str_replace('://', '', 'domain.com/THESTRING'), '/'); // THESTRING
EDIT: This should get you started.
Make sure your server support Mod_Rewrite
I opened a .htaccess in the public_html directory.
Then turned on the Mod_Rewrite by this command:
RewriteEngine on
Then I write a new law:
RewriteRule (.*)\.html$ index.php?link=$1
Thats bicycly mean that every url that ends with .html will redirect to index.php
Then in index.php I get the URL link

Categories