Im starting to devellop a pagination system, and so I´ll need to get some numbers in my URL, but Im having
one problem.
I have a project root folder "project", inside this folder I have:
1 .htaccess file
1 index.php file, that is where I call my getHome() function, to include the correct page and is where I import css, javascripts files, etc
1 folder with name "tpl", and inside it, I have my other index.php file that is my homepage and my other php files (categories.php, contacts.php,...)
To acess my homepage, Im using this url: localhost/project/
And now Im trying to get the numbers I pass in URL with this code:
$page = $url[1];
$page = ($page ? $page : 1);
echo '<h1>'.$page.'</h1>';
The problem is,
If I use this code to get number that I pass in URL in my "categories.php" file, like this: "htttp://localhost/projet/categories/2" -> it's working fine, I get echo of "2" and I have my categories.php file included, but wih one problem, I have some images im my categories.php file and if I use localhost/project/categories I have my images included correctly, but If I use localhost/project/categories/test-1 I can get value I pass in my url and my categories page is included but my images dont appear, images just appear in localhost/project/categories.
If I use this code to get number that I pass in URL in my "index.php" file, like this "htttp://localhost/project/2" Im getting my page 404 error "tpl/404.php", that I include in my getHome() function.
Do you see some way, using my function getHome(), how I can get the number I pass in url, using for example localhost/project/3, and have my index.php file included normally, and dont have my 404 page tpl/404.php' included?
And also how I can my solve my images problem with my categories page?
This is my function getHome()
function getHome(){
$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('tpl/'.$url[0].'.php'))
{
require_once('tpl/'.$url[0].'.php');
}
else
{
require_once('tpl/404.php');
}
}
This is my .htaccess file:
RewriteEngine OnRewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Also, If I use index in my URL, like this: htttp://localhost/projet/index/2, it works, I can get my url value of "2" and I have my home page included correctly. But I´m trying to have just my htttp://localhost/project/2 and get the value I pass, in this case "2" with my homepage and not my 404 error page.
Try using array_pop to get the last value of url then check is_numeric
function getHome(){
$url = (isset($_GET['url'])) ? $_GET['url'] : $_SERVER['REQUEST_URI'];
$url = explode('/', $url);
$template = $url[0] == NULL ? 'index' : $url[0];
$last = array_pop($url);
$page = (is_numerica($last)) ? $last : 1;
if ($template == 'index') {
return $page;
}
if(file_exists("tpl/$template.php")) {
require_once("tpl/$template.php");
} else {
require_once('tpl/404.php');
}
}
$page = getHome(); // $page is used in index.php
Related
I'm building a social network and my htaccess pretty url isn't loading the css/js/img/php files. The following rules work
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^login$ index.php?act=login [NC]
RewriteRule ^register$ index.php?act=register [NC]
RewriteRule ^forgot$ index.php?act=forgot [NC]
But the following rule works but doesn't load the css/js/img
RewriteRule ^user/(\w+)/?$ index.php?act=profile&user=$1 [NC]
What I'm I doing wrong? I don't have a base url set. The home page url is
http://localhost/myWebsite/source/
I also have the following logic in my controller which is included at the top of the index page
if ($isLogged == true) {
// check if page request set
if (isset($_GET['act'])) {
// valid pages
$pgeArray = array("stories", "hashtags", "media", "user", "search");
// check if page is valid
if (in_array($_GET['act'], $pgeArray)) {
// get page name request
$pgeReq = $_GET['act'];
}else {
// show registration page
$pgeReq = "home";
}
}else {
// show registration page
$pgeReq = "home";
}
}
else {
// check if page request set
if (isset($_GET['act'])) {
// valid pages
$pgeArray = array("register", "login", "forgot");
// check if page is valid
if (in_array($_GET['act'], $pgeArray)) {
// get page name request
$pgeReq = $_GET['act'];
}else {
// show registration page
$pgeReq = "login";
}
}else {
// show registration page
$pgeReq = "login";
}
}
All I had to do is specify a base url <base href="http://localhost/myWebsite/source/" />
`
I'm trying to do my site like this tutorial http://www.codeigniter.com/user_guide/tutorial/static_pages.html
but have some problem with routing. I have page "createBook" for default and when I call localhost it's work! But when I do like localhost/createBook I have Error 404. What I'm doing wrong?
In my controller:
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->load->view($page);
}
routes.php file
$route['default_controller'] = 'Books/view';
$route['(:any)'] = 'Books/view/$1';
And I have view files in my views folder named Success and createBook
I don't really get your question but it seems to me that you are trying to call a controller named createBook from the url, but in your function it shows that if there is no views/createBook.php than show 404, maybe its why you get a 404, because your calling a controller as a view, i think your function should be like this :
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'controllers/' . $page . '.php'))
{
// Whoops, we don't have a page for that!
$this->output->set_status_header('404');
show_404();
}
$this->load->view($page);
}
If you want your url to look something like this :
example.com/view/book/3
Your controller should look like this :
class View extends CI_Controller
{
public function book($book_id)
{
//Search the database for records about a book with its id, and return the data
//and assign it to a variable(in this case $data) ex : $data["book-name"] = ...
//And on your view you can call these values using ex : echo $book-name
$this->load->view('books_view', $data);
}
}
And make sure your .htaccess file next to the index.php looks like something like this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Then go to your config, and change this line to :
$config['index_page'] = '';
Now you can access all of your controllers this way :
http://www.example.com/controller_name
so I'm editing a PHP site (I'm a Python guy). What I need is a way to maybe redirect such that when someone navigates to the site e.g. www.mysite.com, the homepage should be served. The system used to serve other pages is as sush: to navigate let's say to the contacts page, we use www.mysite.com?page_id=contact-us. The query string helps the server side code to know what page to serve. So what I want is that when a user navigates to the site by typing www.mysite.com, he should get to the page www.mysite.com?page_id=home.
Thank you.
Sample code:
$page = isset($_GET['page_id']) ? $_GET['page_id'] : null;
if ($page !== null) {
//redirect to correct page
require("modules/inside.php");
} else {
//redirect to 'home'
header('Location: https://www.ndovucard.com?page_id=home');
require("modules/home.php");
}
Try...
if (!isset($_GET['page_id'])){
header('Location: www.mysite.com?page_id=home');
}
Something like this?
$page = (isset($_GET['page_id'])) ? $_GET['page_id'] : 'home';
echo $page; // 'home' or provided page_id
From what you've said it sounds like you need a:
if (!isset($_GET['page_id'])) $_GET['page_id'] = 'home';
Personally I prefer a full URL rewrite system rather than passed by URL query as it's believed search engines prefer contact-us.html rather than page_id=contact-us
Edit:
You could alternatively do:
if (!isset($_GET['page_id'])) {
header('Location: /?page_id=home');
die(); // stop any further processing
}
something like:
$page = isset($_GET['page_id']) ? $_GET['page_id'] : 'home';
/* check that page is a valid page else serve error page */
/* then redirect to the correct $page */
header('Location: www.mysite.com?page_id=' . $page);
exit();
By the way, you need some way to check that $_GET['page_id'] is always a valid page_id before redirecting.
Ok, am using traditional php, no frameworks, nothing, I am using simple procedural way, now my question is I was searching for a while but am not getting an answer to my question, I am not using .htaccess files as of now, but I really need to understand how 404 error works? I am having a website, where I show post's related to category, say category=php, so I pass this as a get request
$_GET['category'] == 'php';
Now currently what am doing is something like this :
$pocategory = $_GET['category'];
if($pocategory == 'php' || $pocategory == 'javascript') {
//Then show related posts
} else {
header('Location:404.php');
exit;
}
I mean I just want php and javascript as valid request's value, rest I want to redirect to 404 but am not understanding how to do it so I did this way, what if am having more than 50 categories? I cant list them all in this if condition, Inshort how to detect whether the given get request value is invalid or not..
Any help will be much appreciated.
.htaccess is the way to do this.
ErrorDocument 404 index.php?404
that line will tell apache what file to load. The example above calls the main index.php script.
add something like this to the top of your index.php file:
$error_404 = isset($_GET["404"]) ? true : false;
now you can detect if you have a 404 error request. $error_404 will be true, so why not add a simple function:
function error_404($error_404)
{
if($error_404 == true)
{
// do some error stuff here, like set headers, and some text to tell your visitor
}
}
now just call your function:
error_404($error_404);
best to do that immidiatley after the get handler:
error_404($error_404)
$error_404 = isset($_GET["404"]) ? true : false;
or combine the two into one line:
error_404($error_404 = isset($_GET["404"]) ? true : false);
to address the question, add this to the relevant script:
$pocategorys_ar = array("php","javascript");
if (!in_array($pocategory, $pocategorys_ar))
{
error_404(true);
}
Make sure it has access to the error_404() function.
You could put all categories inside an array like this:
$pocategories = array
(
'php',
'javascript'
);
if (in_array($pocategory, $pages))
{
// ...
}
else
{
header('Location:404.php');
}
Another thing you could do is creating a html/php file for every category and do it like so
if (is_file('sites/' . $popcategory . '.php')
{
include('sites/' . $popcategory . '.php');
}
else
{
header('Location:404.php');
}
Making a website and I want to put in a custom profile URL for all the users on my site (like facebook).
On my website already, people have a page like http://sitename.com/profile.php?id=100224232
However, I want to make a mirror for those pages that relates to their username. For example, if you go to http://sitename.com/profile.php?id=100224232 it redirects to you http://sitename.com/myprofile
How would I go about doing this with PHP and Apache?
No folders, no index.php
Just take a look at this tutorial.
Edit :
This is just a summary.
0) Context
I'll assume that we want the following URLs :
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Create a .htaccess file in the root folder or update the existing one :
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What does that do ?
If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.
2) index.php
Now, we want to know what action to trigger, so we need to read the URL :
In index.php :
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Now in the profile.php file, we should have something like this :
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
To conclude
I wish I was clear enough. I know this code is not pretty, and not in an OOP style, but it could give some ideas...