issues with friendly urls using htaccess and php - php

i have a problem, but may be easier if i put my code. I have this:
<?php
$view = "";
if(isset($_REQUEST["view"]) != "" && $_REQUEST["view"]) {
$view = $_REQUEST["view"];
} else {
$view = "";
}
#View Handler
$factory = new \API\Factory();
$factory->Template('header');
switch($view) {
#Home
case '':
$factory->View("home/index");
break;
case 'about-us':
$factory->View("home/about-us");
break;
case 'contact':
$factory->View("home/contact");
break;
#Products
case 'products':
$factory->View("products/index");
break;
case 'details':
$factory->View("products/details");
break;
#How To
case 'how-to':
$factory->View("how-to/index");
break;
#Tech Documents
case 'tech-docs':
$factory->View("tech-docs/index");
break;
#Virtual Home
case 'virtual-home':
$factory->View("virtual-home/index");
break;
#Shopping Cart
case 'shopping-cart':
$factory->View("cart/shopping-cart");
break;
case 'checkout':
$factory->View("cart/checkout");
break;
#Client
case 'client-dashboard':
$factory->View("client/client-dashboard");
break;
case 'client-profile':
$factory->View("client/client-profile");
break;
case 'logout':
ClientLogin::doLogout();
header("Location: index.php");
break;
}
$factory->Template('footer');
Don´t ask why, lol. So my problem is this, when user navigates through website he see url's like this: www.mysite.com?view=products, but i want that they can see something like this: www.mysite.com/products/ but i want to manage it using .htaccess how can i implement that .htaccess to rewrite those ugly url's into nice url's. And asuming that all view requests are handled through that index.php
Also want to see it in my localhost, http://localhost:8080/index.php?view=products to http://localhost:8080/products/
Thanks in advance.

you can do this (replaced $_REQUEST["view"] by $_SERVER['PATH_INFO']):
if(isset($_SERVER['PATH_INFO']) != "" && $_SERVER['PATH_INFO']) {
$view = $_SERVER['PATH_INFO'];
} else {
$view = "";
}

Related

After adding detection of browser language, the language href anchor (hl) stops working

I know how to detect language browser in PHP and also how to detect the language href anchor for switching to another.
My old codes which doesn't have locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);:
if (isset($_GET['hl']))
{
$langOption = $_GET['hl'];
}
else
{
$langOption = '';
}
switch ($langOption):
case 'ca':
$language = 'cat';
break;
case 'el':
$language = 'el';
break;
case 'en':
$language = 'en';
break;
case 'en_GB':
$language = 'en_GB';
break;
case 'es':
$language = 'es';
break;
case 'fr':
$language = 'fr';
break;
case 'ka':
$language = 'ka';
break;
case 'nl':
$language = 'nl';
break;
case 'pt_PT':
$language = 'pt_PT';
break;
case 'pt_BR':
$language = 'pt_BR';
break;
case 'ro':
$language = 'ro';
break;
default:
$language = 'pt_BR';
break;
endswitch;
require_once("idiomas/{$language}.php");
These old codes worked very for language href anchors. Like:
<a class="waves-effect" href="?hl=en_GB" name="en">English</a>
<a class="waves-effect" href="?hl=pt_BR" name="pt_BR">Português Brasileiro</a>
I changed to codes to condense, economise and shorten the codes. Here are new codes:
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$lang = $_GET['hl'];
}
else
{
$lang = '';
}
switch ($lang):
case $language:
$language = $language;
break;
default:
$language = $language;
break;
endswitch;
require_once("idiomas/{$language}.php");
Only the detection of browser language worked very well, but the language href anchors (hl) do not work, because if you switch to Portuguese, the page still in English.
Similar like:
switch ($lang):
case "en":
$language = "en";
break;
case "en_GB":
$language = "en_GB";
break;
case "pt_BR":
$language = "pt_BR";
break;
case "pt_PT":
$language = "pt_PT";
break;
endswitch;
But I wouldn't like to repeat these old codes. I want to keep condensing and shortening the same new codes.
I don't understand what the switch statement is doing here?
The reason it's not working is because you're not using the $lang variable for anything, it's just setting $language to itself for all cases.
I think this would do what you want :
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$language = $_GET['hl'];
}
As query params can be modified by the user, I'd also suggest using in_array to check that the value is something expected.

PHP Getting all file names in dirctory for page switching

Right now I have all my pages under a switch case for when a visitor uses whatever.php?p=pagename
Here is how it is currently
switch($page)
{
case 'about':
include('pages/about.php');
break;
case 'contact':
include('pages/contact.php');
break;
case 'edb':
include('pages/edb.php');
break;
case 'eluna':
include('pages/eluna.php');
break;
case 'mercsys':
include('pages/mercsys.php');
break;
case 'pastebin':
include('pages/pastebin.php');
break;
case 'projects':
include('pages/projects.php');
break;
case 'sites':
include('pages/sites.php');
break;
case 'soon':
include('pages/soon.php');
break;
case 'sqlgen':
include('pages/sqlgen.php');
break;
case 'wcms':
include('pages/wcms.php');
break;
case 'add':
include('pages/add.php');
break;
case 'edit':
include('pages/edit.php');
break;
case 'delete':
include('pages/delete.php');
break;
case 'moveAnnouncement':
include('pages/moveAnnouncement.php');
break;
default:
include('pages/404.php');
}
My question is, How can I shorten this down to a foreach loop and use the page names for each .php in the pages/ directory without having to add each individual one or any future pages?
upd: like #svrnm adviced, you can do some security checks if you not did it before:
$filename = realpath('pages/' . $page . '.php');
if($filename && file_exists($filename)) {
include($filename);
}
or/and you can build files whitelist first:
$whitelisted = glob("*.php");
if(in_array($page . '.php', $whitelisted) && file_exists($filename)) {
include($filename);
}

PHP . $_GET and switch case to navigate url from one page to another

I am working on a php base online forum Sincerely speaking i bought the script from codecanyon am still a newbie in php the index page contain $_GET nd switch case which will help in navigating to the other pages but its working keep showing page not find. I have tried all i can pls i need your help thanx.....
`<?php
include("includes/db_config.php");
include("includes/google_config.php");
include("includes/functions.php");
include("includes/loaders.php");
//get web settings
$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id
DESC LIMIT 1"));
//update user online time
if($_SESSION['usern']) {
$user_id = userinfo($_SESSION['usern'],"id");
$online_time = time();
$update = mysql_query("UPDATE users SET online_time='$online_time' WHERE
id='$user_id'");
}
//update forum visits
update_visits();
load_header();
$page = protect($_GET['page']);
}
switch($page) {
case "set_password": include("pages/set_password.php"); break;
case "chat_content": include("pages/chat_content.php"); break;
case "chat": include("pages/chat.php"); break;
case "tag": include("pages/tag.php"); break;
case "forum_sign_in": include("pages/sign_in.php"); break;
case "forum_sign_up": include("pages/sign_up.php"); break;
case "forum_lostpassword": include("pages/lostpassword.php"); break;
case "forum_profile": include("pages/profile.php"); break;
case "forum_messages": include("pages/messages.php"); break;
case "forum_online_users": include("pages/online_users.php"); break;
case "forum_adpanel": include("pages/adpanel.php"); break;
case "view_forum": include("pages/view_forum.php"); break;
case "view_thread": include("pages/view_thread.php"); break;
case "post_thread": include("pages/post_thread.php"); break;
case "post_replie": include("pages/post_replie.php"); break;
case "post_edit": include("pages/post_edit.php"); break;
case "post_delete": include("pages/post_delete.php"); break;
case "post_quote": include("pages/post_quote.php"); break;
case "post_report": include("pages/post_report.php"); break;
case "userinfo": include("pages/userinfo.php"); break;
case "search": include("pages/search.php"); break;
case "read_message": include("pages/read_message.php"); break;
case "send_message": include("pages/send_message.php"); break;
case "reply_message": include("pages/reply_message.php"); break;
case "delete_message": include("pages/delete_message.php"); break;
case "panel": include("pages/panel.php"); break;
case "adpanel_func": include("pages/adpanel_func.php"); break;
case "forum_logout":
unset($_SESSION['usern']);
session_destroy();
session_unset();
$redir = $web['forum_url']."sign_in/";
header("Location: $redir");
break;
default: include("pages/home.php");
}
load_footer();
?>
First you have to debug whats inside the $page variable with:
var_dump($page);
and look whats is the value when you click that link.
NOTE:
I see a brace "`" before the php tag opener, delete it

How can I pass this variable and check if it matches?

How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}

Send value to php file

I'm try to send a value to a PHP file, but when I check, this value became null.
I send the value by: user_login.php?p_action=New_User
The code of user_login.php is:
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
switch ($p_action) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
}
case 'Save_Profile': {
$l_flag = "Save_Profile";
break;
}
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
}
switch ($l_flag) {
case 'New_User': {
include "include/user_new_inc.php";
break;
}
case 'Save_Profile': {
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
}
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
First your code isn't correct please read more about using Switch here
second to access to any variable came from url you can use Global variable $_GET or $_REQUEST
and you can read more about them from here and here
and this is your code after fixing it please try to run it
<?php
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
$p_action=$_GET["p_action"];
switch ($p_action) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
case 'Save_Profile':
$l_flag = "Save_Profile";
break;
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
break;
}
switch ($l_flag) {
case 'New_User':
include "include/user_new_inc.php";
break;
case 'Save_Profile':
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
?>
you need to make the code like this friend
switch ($_GET["p_action"]) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
that well give you the value of the get!!!
Use $_GET to get your parameter.
Sometimes $_REQUEST is preferable since it access both get & post data.
2nd thing never trust the user input so you must use addslashes(); or real_escape_string() function to prevent attacks on the system.
So Code would be like this :
$var = addslashes($_GET['p_action']);
switch($p) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
"OTHER CASES HERE"
}
Notice that : Don't add { } for CASE. Read syntax for switch
here.

Categories