Concept - Serving dynamic content based on URL path? - php

For example,
the user types in,
www.foob.com/some_path
I don't want to actually have a some_path folder for every different identifier the user might use ... for example if it is a person's name, as seen in many social apps.
www.foob.com/some_path1
However, i would like to read it in as a variable and serve the relevant content
JavaScript
var foo = 'some_path1'
PHP
$foo = 'some_path1';
How do I get 'some_path1' into foo?
Note
some_path is a variable, it might contain
some_path_1
some_path_2
some_path_3
Clarification
So take facebook.com
if you go to
www.facebook.com/your_name
it's not like there is a different directory for all 1 billion users.
A generic page is served populated with user specific data.
How do I get the id your_name?

You need to setup www.foob.com/some_path to point to a PHP script URL with index.php?q=some_path or similar in your .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?q=$1
Then it's just a matter of serving different content based on $_GET['q'] in the PHP file.
or to use it in Javascript:
echo '<script>...var q = ' . $_GET['q'] . ';</script>';

You can use $_SERVER['REQUEST_URI'] to get this information out with PHP. If you want it in JavaScript, take a look at the window.location object (probably window.location.href or window.location.pathname). Both might require a bit of parsing to only grab the part that you want, though.
Regarding the rewrite rule to send these requests to your script, I'd use something like this, which will redirect all requests for files that aren't found elsewhere through app.php. This should go in an .htaccess file on the root of your site.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

Add a .htaccess file to your root directory.
.htaccess
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [L]
You can add something similar to this at the top of your index.php page:
<html>
<head>
<title> MyWebsite <?php echo '~'.ucwords($desired_page); ?> </title>
<?php
$page_array = array(
"home", "blog", "news",
"links", "about", "contact"
);
$desired_page = (isset($_REQUEST['page'])) ?
$_REQUEST['page'] : "home";
if(!in_array($desired_page, $page_array))
$desired_page = "error";
?>
</head>
<body>
<div id="menu">
<ul>
<li <?php if($desired_page=="home")
echo("class=\"current\""); ?>>
Home</li>
<li <?php if($desired_page=="blog")
echo("class=\"current\""); ?>>
Blog</li>
<li <?php if($desired_page=="news")
echo("class=\"current\""); ?>>
News</li>
<li <?php if($desired_page=="links")
echo("class=\"current\""); ?>>
Links</li>
<li <?php if($desired_page=="about")
echo("class=\"current\""); ?>>
About Us</li>
<li <?php if($desired_page=="contact")
echo("class=\"current\""); ?>>
Contact Us</li>
</ul>
</div><!--end menu-->
<div id="content">
<?php
/*Includes specific page content*/
include("content/".$desired_page.".php");
?>
</div> <!-- end #content -->
</body>
</html>

Related

404 function is working but not showing properly

So i had .htaccsess file with the following code
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
# Allowed the url has access to the php file without the .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^folder/?$ - [F,L]
# Force remove .php extension, if manually changed in url
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</IfModule>
Options All -Indexes
ErrorDocument 403 /AllProject/layouts/403-page.php
ErrorDocument 404 /AllProject/layouts/404-page.php
ErrorDocument 500 /AllProject/layouts/500-page.php
So I want to create a restriction for users who don't have access to a certain url. When the user tries to access a certain url via the url address, the user will be redirected to a 404 page.
The function works but the 404 page doesn't display correctly, instead of using the error page from .htacess, but the standard 404 error from apache, the following code is
header function
<?php
// Check role
if ($_SESSION['role'] != 'ADMIN') {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
die;
if (!isset($_SESSION['username']) && !isset($_SESSION['role'])) {
$containUrl = urlencode((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
header("Location: index?destination=" . $containUrl . "");
}
}
Then I tried using the require function, but that's also a problem because on the 404-page.php page I'm using require function too. Which make the file become to contradict. The file code is
404-page.php
<?php require '../koneksi/koneksi.php'; ?>
<?php require 'resources.php'; ?>
<body>
<!-- Page Container -->
<div class="page-container">
<div class="error-404">
<div class="error-bg"></div>
<div class="error-info">
<div class="error-text">
<div class="error-header">
<h3>404</h3>
</div>
<div class="error-body">
<p>Halaman yang anda cari tidak ditemukan.<br>Klik disini untuk kembali.
</div>
<div class="error-footer">
<p><?= date('Y') ?> © ReD | Projects <?= $version ?></p>
</div>
</div>
</div>
</div>
</div><!-- /Page Container -->
<?php require 'footer.php'; ?>
So where is the problem?
u try this code in .htaccess
#404 page
ErrorDocument 404 http://localhost/not-found.php
So, i've just figured it out to solve my problem.
Thanks to #CBroe at the comment to suggest me using DIR
It's actually working fine for me now.
So now in the 404-page.php now i am using
<?php require_once __DIR__ . '/../koneksi/koneksi.php' ?>
To require the file outside properly.

Navigation menus not working as expected in CodeIgniter

I am using CodeIgniter v3.1.3. Based on the 'Static Page' tutorial, I am trying to create a static website containing 5 pages such as Home, About, Services, Portfolio, and Contact.
I have a Pages.php controller inside 'application/controllers' directory.
My Pages.php controller looks like
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
//Controller logic
public function view($page = 'home')
{
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
//Whoops!, we don't have a page for that
show_404(); //In-built CI function to show 404 error pages
}
$data['title'] = ucfirst($page); //Capitalize the first letter
$this->load->view('templates/header',$data);
$this->load->view('pages/'.$page,$data);
$this->load->view('templates/footer',$data);
}
}
I have also created the 5 static pages inside 'application/views/pages' like home.php, about.php, services.php, portfolio.php, contact.php
Common codes of header & footer exists at 'application/views/templates' like footer.php & header.php.
My navigation menu inside header.php looks like
<li><a class="curr_menu" href="<?php echo base_url('pages/view/home');?>">Home</a>
</li>
<li><a class="page-scroll" href="<?php echo base_url('pages/view/about');?>">About Us</a>
</li>
<li><a class="page-scroll" href="<?php echo base_url('pages/view/services');?>">Services</a>
</li>
<li><a class="page-scroll" href="<?php echo base_url('pages/view/portfolio');?>">Portfolio</a>
</li>
<li><a class="page-scroll" href="<?php echo base_url('pages/view/contact');?>">Contact Us</a>
</li>
My routes.php(application/config/routes.php) looks like
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
The homepage is shown initial load, however when I click on any menu items, it returns '404 Page Not Found' error.
Can someone please explain - with clear code - show to make my menu work.
.htaccess at the root contains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I highly recommend you to manually navigate to the wanted pages by writing the URL by hand in the address bar to make sure the error comes from the link building.
If so, then try using
<?php echo site_url('pages/view/home');?>
Probably your url is coming out wrong. Your default controller is
"Pages/view " ??
If so, then the navigation menus should be like this :
<li><a class="curr_menu" href="<?=base_url()?>home">Home</a>
</li>
<li><a class="curr_menu" href="<?=base_url()?>about">About Us</a>
</li>
<li><a class="curr_menu" href="<?=base_url()?>services">Services</a>
</li>
<li><a class="curr_menu" href="<?=base_url()?>portfolio">Portfolio</a>
</li>
<li><a class="curr_menu" href="<?=base_url()?>contact">Contact Us</a>
</li>
And in the config.php file,
$config['base_url'] = 'http://yoururl/'; //should be followed by forward slash (/)
$config['index_page'] = '';
In order to use base_url() you have to load url helper first in your controller.Like this
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
Or you can load it in application/config/autolaod.php.Like this...
$autoload['helper'] = array('url');
And don't forget to set bae_url at application/config/config.php
$config['base_url'] = 'your_url';
Also
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
In your root folder. .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

wordpress page template is not working

I am working on theme and i want to create page template may code is below :
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div id="content">
<div class="left">
<div class="pagina">
</div>
</div>
</div>
<?php get_footer(); ?>
I have creat page name Blog and assign it a custom template from dropdown Blog,But when I click on navigation element Blog it shows The requested URL /wp/blog/ was not found on this server.
anyone can help?
I'd suggest this is a permalink issue, not a template issue. Your .htaccess file should be writable and you should have some code automatically added to it when you set up the permalinks. Check that it is writable and try setting the permalinks up again. Failing that, I believe that WordPress gives you the code to copy/paste into it when you're setting up the permalinks.
In short, add this code to your .htaccess file, if it's not there already.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
That could be one of many reasons; either you have a blog template or it could be because you have not included the loop:
<?php
/*
Template Name: Archives with Content
*/
?>
<?php get_header(); ?>
<div id="content" class="">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<div class="left">
<h2 id="post-<?php the_ID(); ?>"><?php the_title();?></h2>
<div class="entrytext">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<div id="main">
<?php get_search_form(); ?>
<!-- and whatever else you need -->
</div>
<?php get_footer(); ?>
If that still does not work, try refreshing your permalinks

why isn't index.php working?

I have a single index.php page that should link to the rest of my websites.
<?php
if(!isset($_GET['page']))$page = 'home.php';
else{
$page = $_GET['page'] . ".php";
}
include("_includes/header.php");
include("_includes/navigation.php");
include("_pages/$page");
include("_includes/footer.php");
?>
This is what my index.php looks like. It's supposed to get the page i click on (when i clikc on the navigation) and direct it to this page so that the header, navigation, and footer will be reused for every page.
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
according to the code the "home" link should work but it says URL not found because it is looking for "home" in the directory that I'm in. however its in my pages directory. "skills" does the same thing. the only ones that do work is the links projects through contact because i am specifying a path. however, the links that work don't go through my index page because the header.php (which contains the css), navigation.php, and footer.php is not included here.
i did research and i found that i had to do something with the .htaccess file and/or something with mod_rewrite. can someone please help me out. I've been looking for about 2-3weeks now and nothing.
You should be referencing index.php, not home..
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
Your links should be:
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
It's strectly related on how your web server handles requests.
If you try to visit url that's not handeled by PHP you won't get it to work. By deafult PHP handles only .php request url (also .php5 and some others).
A solution could be to write a rewrite condition that convert your /home/ to index.php?page=home. This assuming you're running Apache Web Server with mod_rewrite enabled and htaccess enabled.
So, supposing to normalize your links this way:
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
you can write an .htaccess like this, and put it in /pages/ folder:
RewriteEngine On
RewriteRule ^/pages/(.*)$ /index.php?page=$1 [QSA,L]
Beware that every request to /pages/ will be rewrited, so you should write them carefully avoiding rewrite of .css .png and so on...
This will effect your request this way:
Apache will recive request for a file in /pages/ named home
RewriteRule will detect it with the regexp
The rule forces apache to answer with your index.php with the parameter page=home (or whatever the regex has catched)
P.S.
Maybe you could be interested in flag P to send request to index.php as a Proxy, but for further infos you should refer to mod_rewrite docs (http://httpd.apache.org/docs/current/mod/mod_rewrite.html)

Creating links, and creating content for the links, on the fly, php

I have a index.php, it calls a function post_themes_front(), the function gets all the "posts" from the SQL database and creates all the content, including the links. Example: picture A links to www.index.com/picture_a_name
code sample:
while($row = mysql_fetch_array($result))
{
echo '
<div class="post">
<h2>
'. $row['name'] .'
</h2>
<div class="infos b20">
By <em>MeetThemes</em> on <em>'. $row['date'] .'</em>
<span class="label label-important pull-right">'. $row['views'] .' views</span>
'. $row['comments'] .'
'. $row['catagory'] .'
'. $row['catagory'] .'
</div>
<p>
<a href="http://www.meetthemes.com/'. $row['name'] .'" title="ThemeForest - Silicon - Responsive WordPress Theme">
<img alt="ThemeForest - Silicon - Responsive WordPress Theme" src="img/'. $row['image_path'] .'" class="post_img" />
</a>
</div>
';
}
There is no such file as "picture_a_name", id like to create that, when the user clicks on it, and send him there with the apropriate values for comments, link etc, fetched from SQL.
But is it to late to create it on click?
Does it have to be created before that? i could make a function to create the content of all posts in index, and call it on index, and send them there on click but that would take up quite alot of client resources ..... in therms of loading time (I know its server side)...
Any suggestions are welcome
Example of what i want to achive, newone.org the frontpage, contains loads of "posts" which all link to seperate content.
I understand that there are plenty of CMSes that will do this for me, but i dislike using drupal / joomla / wordpress
What you can do is make an .htaccess file which catches all requests to non-existing files, something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ createContent.php?file=$1 [QSA,L]
</IfModule>
So that, when 'picture_a_name' doesn't exist, the request is redirected to createContent.php?file=picture_a_name. In createContent you can now create the asked content and save it to 'picture_a_name'.

Categories