Can I hide a portion of my URL? - php

I asked a simialr question earlier about using jquery and hashtags but what I really need to do is after a successful login redirect the user to the home page (home.php) Ideally I want to load my header with my menu only once and then update my main div tag with the contents of home. This will let me load the menu bar just once and not after every page load. I'm currently calling the home page like so:
if(isset($success) {
header('Location: menu.php?home');
exit();
}
Is it possible to hide the menu.php portion of the URL to the end user or is there a better way?
Thanks

Maybe look at apaches mod_rewrite. Tutorial

Is it possible to hide the menu.php portion of the URL to the end user?
No.
is there a better way?
You can use a mod_rewrite rule to rewrite /home to /menu.php?home.

what about renaming menu.php to index.php & using it like
if(isset($success)) {
header('Location: /?home');
exit();
}

Related

create website without an home page

I would like to create a website without an home page
example for home page will be
www.lawyer.com
instead i would like to present only clean urls like this
www.lawyer.com/lawyer-in-us-chicago
www.lawyer.com/lawyer-in-us-denver
etc...
my first question is
how will it affect on my SEO?
do i must have an home page like
www.lawyer.com
even if i don't have any content to put on it
my second question is
how should i redirect from
www.lawyer.com to www.lawyer.com/chicago-lawyers
Just put a blank page called index.html, and inside it write the following JavaScript code (inside <script></script> tag):
window.location.href = "www.lawyer.com/chicago-lawyers";

PHP and web page displaying

I have searched all over the web trying to figure this out and am now trying to get a direct answer from some experienced users. I hope I can explain myself completely.
I know HTML and CSS and some PHP and Javascript, but no mean an expert. This is my questions:
When creating a website by hand (no Drupal, or Wordpress or predesigned templates), The first thing I do is create an index.php file that shows my HTML page layout. The second thing I do is create my links.inc.php file that will show all the links to my pages, ex: Home, About Us, Contact Us. Now on the index.php page I create php include files for the header, footer, and link pages. (these would read header.inc.php, footer.inc.php, links.inc.php) Now here is where I am trying to figure if there is an easier way to do the next step.
My normal steps would next to be to create a home.inc.php, aboutus.inc.php, contactus.inc.php files which will have all the "content" I want shown for each page.
I would then create a duplicate of the index.php and create aboutus.php where I would use the php include function to add the aboutus.inc.php into the "main content" area I would want this information displayed at. Then I would create anther duplicate of the index.php and name it contactus.php and "include" the contactus.inc.php file.
Is there any way to use the index.php file and have all the inc.php files on that page? For instance,
<div id="main">
<?php
include ("home.inc.php");
include ("aboutus.inc.php");
include ("contactus.inc.php")
?>
</div>
Obviously this does not work they way I have it laid out above, it shows all the pages at the same time instead of only showing the one page that is clicked on from the menu. Any suggestions? Is there a different way or am I doing it correctly with creating multiple pages?
Thank you for any help and I hope I was clear, if not I can try to explain a different way.
My suggestion is to include files conditionally, based on a variable that defines the current page.
For example, given the following navigation:
Home
About Us
Contact Us
Configure your index.php file to include external files, something like this:
// determine the requested page, default to the home page
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// check if the requested include file exists
$include_file = is_file($page.'.inc.php') ? $page.'.inc.php' : false;
// if the requested include file exists, include it
if ($include_file) {
include $include_file;
}
Feel free to adjust the logic. For example, if a $page value is not recognized as a valid page on your site, you may want to show a 404 page, default to the "home" page, etc.
Edit
If your include files are in a different directory, you'll need to provide the correct path:
// define the path to includes
$path = 'inc/';
// check if the requested include file exists
$include_file = is_file($path.$page.'.inc.php') ? $path.$page.'.inc.php' : false;
You could send a variable to PHP index.php?action=home; then, inside index make some verifications
if($action=="home") {include index.inc.php; }
else if ($action=="contact") {include contact.inc.php }
and so on.

PHP Dynamic Webpage

I'm attempting to create a dynamic website where certain site content loads in to index.php's body.
I currently have the site divided in to 3 sections: Header, Body, and Footer where each of these sections are dynamically separate from each other.
<?php
if(!file_exists('content/header.php'))
{
die('Sorry we are experiencing technical difficulties. Please come back later.');
}
else
include('content/header.php');
?>
<?php
include('content/body.php');
?>
<?php
include('content/footer.html');
?>
Now what I'm hoping to do is have certain page content load in to body.php if lets say I click on a hyperlink that says "register" so that a user my register them selves to the site, register.php will load its contents in to body.php.
I've tried searching around but I think I'm asking the wrong questions in Google search so I figured I'd explain my self here and hopefully someone would guide me in the right direction, Thank you for your time.
If you want it so the user doesn't get redirected look at AJAX.
Otherwise you could have the register redirect to index.php?page=register and your code be
<?php
$page = isset($_GET['page']) ? 'body' : someSanitizingFunction($_GET['page']);
include('content/'.$page.'.php');
?>
That's obviously a unsecure implementation but you'll get the idea from it.
You should also look at a template engine like Smarty, that may be what you're after.
To create simple layouts in PHP you can write pages like about.php, contact.php and register.php, then
you can put this code in your body.php
$whitelist = array("about", "register", "contact");
if(in_array($_GET['page'], $whitelist)) {
include($_GET['page'].".php");
} else {
include("error404.php");
}
Your url should be like index.php?page=register.
I also recommend to have a look in MVC frameworks with Smarty, Twig or TemplatePower. It is a good way to sort with layouts.

PHP Get root directory of website

I am making a site and I need to get the "root" directory of the site. Like if the filestructure was similar to
CloudShop
\-internal
\-js
...
\-inc
inc.all.php
index.php
\-login
login.php
loginError.php
page.php
In inc.all.php I am getting all the pages in the database so I can display them in the navigation bar and I am setting the links like <a href="page.php?id=XXX>PAGE_TITLE</a> However, when I click on one of the page links from login.php, it takes me to /CloudShop/login/page.php?id=XXX. I want it to take me to /CloudShop/page.php?id=XXX.
You can use path-absolute URIs. Suppose you had at http://www.example.com/shop/login/login.php:
page
The resolved URI would be http://www.example.com/shop/page.php?id=1.
Suggestion: Try the PHP Magic constants:
__DIR__

Can I modify anchor tag href in htaccess?

Not sure if this is possible but wanted to know if htaccess has a trick...
Say I have anchor tag href like this -
Click here
I have changed the URL structure using mod_rewrite but wanted to know if i can hide the actual URL in href using htaccess.
i.e when a user hover over the anchor tag, the browser displays http://example.com/index.php?id=12345 at the bottom. All I want the browser to display is http://example.com/index/12345 without changing it manually on all my pages.
Thanks for the help !
Why don't you change the link to the following?
Click here
As you can change the .htaccess I expect that you own or adminstrate this domain. So it should be possible.
If the links are generated by PHP code, then I suggest you to implement and use a translation function like:
function beautify($ugly) {
// your logic comes here
return $nice; // ;)
}
... and wrap it around the existing code that currently outputs the urls. This would have two advantages:
It's easy and more failsafe to migrate to the new url scheme
From now on you have control over all url related code using a single function
I agree, htaccess can't help you. I guess you'll have to change them manually.
I wish I could be of more help
No. htaccess is for processing input to the web server, not data sent back from the server.
If you use jQuery you could have it rewrite the href when the page loads using something like this.
$(function(){
$("a").each(function() {
this.href = 'some_new_url that you made based on the one in this.href';
});
});

Categories