This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 8 years ago.
How can i create pages in php, but don't create physical pages. The page don't exist in disk.
I don't want create a physical page in disk for each page ... Image with 3 thousand pages ...
Well, that's really general question. I assume that you're a beginner. Sorry if you're not but that what it seems to me like.
There are like 100 ways of creating website using PHP. PHP is there so you don't have to, like you said, create separate file for each page. If you did, it would be just a simple templating (you create HTML layout and put little PHP scripts inside so you can generate content dynamically).
I believe this isn't right place to ask. Instead you should look up some tutorials, get the picture of how PHP works in general and than maybe try something out and THAN ask again.
W3Schools.com is a great website to start with. Great tutorials, learned a lot there :-)
I think you want to avoid creating physical files on the disk.
The simplest thing comes in my mind, as you can use $_GET.
Take a look at this code.
index.php :
<?php
// Get the "page" information from HTTP GET.
$page = (isset($_GET["page"]) ? $_GET["page"]; : "default";
// Display content according to the page information from GET.
switch($page) {
case "foo_page":
echo "this is foo page.";
break;
case "bar_page":
echo "this is bar page.";
break;
default:
echo "this is the default page.";
break;
}
?>
Now your index.php will respond
this is the default page.
But when you go to index.php?page=foo_page, you'll see that it outputs
this is foo page.
Then you could rewrite it by a .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)/?$ index.php?page=$1 [NC,L]
Which would make your url's cleaner, like :
domain.com/foo_page/
domain.com/bar_page/
As i've told, this is the first thing i thought of when i read the question.
Hope this helps, or at least gives an idea.
I think you have lot's of images to display in page by page.
Like page1|page2|etccc
if so
First you should plan how it works. In this case , ill do
initialize a variable noofimage=30
another variable start-from=startfrom * finish.
initially start-from=0; // because it start from 0 to noofimage you specified to show in single page.Place this line below nofimage;
do query in php to start from "start-from" to "noofimage"
get total images and divide it by noofimage.
if you have 3000 images then 3000/30=100
so now you have 100 pages.
now using while loop create a page link
int i=1 // start from page 1
while(page<=100)
{
<a href="yoursite.com/images.php?pageno=$i>
i++ // increment page
}
now in your sitebelow
page1|page2|page3|.....
wikk created ....
if some one clicks a page.
at server ,
php files first
$start-from=$_GET['pageno']
$finish= $start-from * $ noofimage;
$start-from=($start-from -1)* $ noofimage
// we are using startfrom- 1 because , in next page images should start from 30 to 60. so we get a page multiple it with noofimages per page for $finish and for start we just decrement 1 then multiple it with noofimage.
now , use php mysql query to (start from , end);
// I just shared a idea how to do php for this page.
try it , or ask if you have any problems.
Related
I would like to include at the beginning of my script a PHP file that open a IF condition. Then i write my script, and to finish I include another PHP file that close the conditon.
This bring me to a "Parse error: syntax error, unexpected end of file in ..." error.
This will be better to understand with this simple example :
header.php
if(aConditionalTest()) {
footer.php
} // endIf
mypage.php
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
FYI: I would like to do this for example :
to check everywhere that a user is authorized before displaying the content
implement a webpage caching system (see http://www.phpfastcache.com/ in "Example" section, "Caching Whole Webpage")
THANKS!
edit : Explain more precisely WHY I want to do this for using phpfastcache :
http://www.phpfastcache.com/ says :
Caching Whole Webpage PHP Cache whole web page :
You can use phpFastCache to cache the whole webpage easy too. This is simple
example, but in real code, you should split it to 2 files:
cache_start.php and cache_end.php. The cache_start.php will store the
beginning code until ob_start(); and the cache_end.php will start from
GET HTML WEBPAGE. Then, your index.php will include cache_start.php on
beginning and cache_end.php at the end of file.
That's just what I try to do!
According to their piece of code below, this brings to the situation where the condition is opened in "cache_start.php" and then closed in "cache_end.php"
cache_start.php
use phpFastCache\CacheManager;
$cache = CacheManager::Memcached();
$keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
// try to get from Cache first.
$resultsItem = $cache->getItem($keyword_webpage)
if(!$resultsItem->isHit()) {
ob_start();
cache_end.php
// GET HTML WEBPAGE
$html = ob_get_contents();
$resultsItem->set($html)->expireAfter(1800);
$cache->save($resultsItem);
}
echo $resultsItem->get();
mypage.php
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
myotherpage.php
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
So the reason WHY I want to put the phpfastcache code in 2 separate files is that I have many different PHP pages to cache, so I would like to avoir repeating all this code on each page...
Hope this edit will help you better understand why I would do that, even if I understood, as I feared, that is is not possible.
Give it a try:
how can I achieve this ?
Do it the evil way and eval all instead of including :) Like
eval(file_get_contents('header.php').'<?php echo "my awesome content";?>'.file_get_contents('footer.php'));
That can be a solution, if you want to join the dark side :)
SideNote: In this solution, you have to keep an eye on global variables!!
But please, thing about the fact, that you want to spread conditions over seperate files, what in my opinion is very very very bad practise.
Did i really answer this 8]
I try it in other way.
Only rule: works only in global space (where else :-))
So you want to open an if() in cache_start.php and close it cache_end.php. (for ob_cache reasons)
But if the condition isn't changed why not doing the condition twice!
In each file test for if(condition)!
Or set up an variabale like $cach_op_started=true and test for it in the second if() in cache_end.php
Thing boths should work for you.
Its a little funny that i didnt see that solution at the first time :)
Last Note:
You can also use auto prepend and append files in PHP if you want to.
That can be configurated in php.ini.
The files will automaticly loaded before and after an script, always.
http://www.webdevsecrets.com/using-phps-auto_prepend_file-and-auto_append_file/
Have a nice time.
So I understand that the problem is that when you don't want a certain user to see the contents of a page, the rest of the page isn't loaded and that is whats causing the error?
If so, why don't you just always include the files and in the specific page you set the conditions for who can view what?
I use ob_start() in my header, and ob_end_flush in my footer which works great.
and then check with my SESSION variables on the specific page's content if the logged in user has the right to see the content, else display a message like:"You are not authorized to see this content"
So I think this is really simple yet I am have trouble making it. I want it to be used 2 ways [so its 2 scripts].
Always redirect url is redirect.com and base.url is base.com [base.com can be swapped to other urls if I need]
1. When I typein: redirect.com/?q=search what it actually does is redirects to base.com/q=search
2. When I typein browser redirect.com/path-to-some-url what it actually does is goes to base.com/path-to-some-url - of course theres unlimited paths of urls.
Could somebody help?
Id prefer pure php file code where I just put base.com and rest happens per url entrance.
This should work. Put this in the index.php file for redirect.com:
<?php
header('Location: http://base.com/q='.$_GET["q"]);
?>
I've been trying this code since 3 days in a row until a certain time (minutes or seconds) but unable to solve the problem.
My target is to redirect visitor to 10 random URLs which are being selected from a text file. The user will see a certain page for a certain time and then redirect to another page again, the number of pages he will be redirected to is complete RANDOM.
PROBLEM:
The problem is the visitor is not being redirected to any other page which is randomly selected from a text file, instead it is just refreshing the page... But I want to redirect him to other pages from the text file.. Hope you guys understood me by now.
EDIT: Found the problem. Actually the $rand_link is having NULL as it's value.. { [0]=> NULL } Don't know why.... ANy solution? Checked the 'BBnormalLinks.txt' file for it's permissions and that file is having some links in it for sure because I just checked it..
Thanks,
Here is the CODE:
<?php // Generate Random Nubmers.. 2 ********
$numbers2 = range(13,70);
shuffle($numbers2);
for ($j=0;$j<1;$j++)
{
$numbers2[$j];
}
$seconds = numbers2[0];
//////// For Random URL of Site
$links = file('BBnormalLinks.txt');
$rand_link = $links[ mt_rand(0, count($links) - 1) ];
header("refresh:". $seconds .";url=". $rand_link); ?>
The syntax is correct but some pointers that can cause this are
Some text have been outputted before the header is passed.
The random page, is not being generated, thus ending up refreshing the same page again and again.
I have a very strong feeling, that your $rand_link is returning blank or null.
Update:
After a few discussion, the problem was the evil path again.
$links = file('patotofile\BBnormalLinks.txt');
As baburao113, quoted
I had to move that file to wordpress theme folder lol! Problem resolved :)
Without knowing the contents of 'BBnormalLinks.txt' or the final value of $rand_link, it is difficult to say precisely what is going wrong.
Your usage of file() and mt_rand() appear to be correct, though you aren't doing anything to ensure that you are getting a valid URL.
This is purely conjecture, but I suspect that you don't have PHP properly configured to display errors. If the file fails to load, $links will have a null value going into your penultimate line. You will then try to access element 0 of a null array and receive an empty value. This will result in header('refresh:2;url=') and your page will simply refresh itself every 2 seconds.
From the little I know about the refresh header, it does not seem to be the most reliable thing to count on. (I'd guess not all browsers respond to it.) Since you are showing content, at least for a few seconds, you could try using window.location in a window.setTimeout in JavaScript instead. So something like:
$content = '<script type="text/javascript">window.setTimeout("window.location=\''.$rand_link.'\';", '.($seconds*1000).');</script>';
How could I make my webpage change its content using get parameter? I saw a bunch of website registrations that when you are successfully registered you'll be redirected to the same page but with get parameters i.e www.register.com?do=success, or something like that. I tend to make another webpage for that but this looks promising. Instead of making another page, I would just change the content using get parameter for success registration page.
Nowadays web applications have more or less only one single entry point (index.php) and avoid different and resource specific files. The reason is that a single file provides more control over how the user interacts with the site. The actual resource to be returned to the user is then specified in the query string (or determined through trailing folders and some rewriting mechanism).
A simple approach to establish a single entry point for your entire site:
index.php
<?php
switch ($_GET['pageId']) {
case 0:
echo "Home";
include "content/home.inc.php";
break;
case 1:
echo "Links";
include "content/links.inc.php";
break;
case 2:
echo "Animated Gifs";
include "content/coolstuff.inc.php";
break;
case 3:
echo "Guestbook";
include "content/guestbook.inc.php";
break;
default:
echo "404";
break;
}
Call:
http://localhost/index.php?pageId=0
http://localhost/index.php?pageId=1
http://localhost/index.php?pageId=2
http://localhost/index.php?pageId=3
http://localhost/index.php?pageId=Hohoho
Create an index.php file; in a normal setup that is the file that will be excecuted when calling something like www.yoursite.com/?do=success. The querystring (the part after the ?) parameters can be fetched through the global $_GET array.
W3C: PHP Tutorial
PHP For the Absolute Beginner
PHP: A simple tutorial
Beginners PHP
You just need use the $_GET['do'] to decide what to response to the browser.
I have a general question about php and mysql.
So currently, I have a form in a html file that on sumbit gets posted to a code.php with some input processing in code.php.
How would I go about generating different pages on submit with mysql and showing them the result from code.php?
So for instance, I have my domain example.com and on submit, it would generate a page example.com/1.php, and after, if I were to refresh the page and hit submit again, it would generate example.com/2.php?
I know my question is very general, just looking for a tutorial, or example to code to follow!
I actually think what you are asking for is something like a step-by-step add process?
So what I write in page 1, page 2 and page 3 firstly wants to be added on page 4?
If that is the case you should read about serialize()-function in PHP.
E.g. on post you could make this:
if( isset($_POST['step1']) )
{
$_SESSION['step1_ok'] = 1;
}
And then use the session for checking if the user comes from the last step. :)
I don't get your point, but to create a file you use fopen.
Not sure the point of this, are you trying to have a form that contains multiple pages?
You could just do that in 1 php page with something like:
<?php
if(isset($_POST['step'])) {
$step = $_POST['step'];
}
else {
$step = 1;
}
if($step == 3) {
//show s3rd page
}
else if($step == 3) {
//show 2nd page
}
else {
//show 1st page
}
?>
If you want the 1.php, 2.php, 3.php,.....
Then you should do something with mod_rewrite to make all those requests to to 'code.php' and then you would do something like above to figure out which of the virtual pages they tried accessing
Generating php code and then saving it as a php file which is then executed opens you up to a lot of security issues.
If your goal is to create the URL as you described for cosmetic reasons, perhaps you should look into mod_rewrite, or something similar.
http://articles.sitepoint.com/article/guide-url-rewriting