i have a large number of files with several id's in each file. For example file1.php contains a number of paragraphs, each paragraph has a unique id. (id="1",id="2",id="3" etc...) I would like the ability to create a link to a page (page A.php) and pass the location of one of these id's in the url of the link to display in a php include on page A.php The result that i'm looking for is to have the entire file (file1.php) show up inside of page A.php with the specific id that is passed in the url being highlighted. Is this possible? or do I need to use Java Script and an iframe?
Here is what I ended up with:
The url: http://mydomain/thispage.php?xul=http://mydomain.com/folder1/folder2/file.php&id=Abc150:176
The code:
Stylesheet .vrsehilite{styling}
<script type="text/javascript">var x = <?php echo json_encode($_GET["id"]); ?>;</script>
<?php
$invdmn = "<h2>Error: Invalid Domain</h2>";
$filnf = "<h2>Error: File Not Found</h2>";
$pthinv = "<h2>Error: The Path is invalid</h2>";
$idinv = "<h2>Error: The ID is invalid</h2>";
$oops = "<br/><h2>Oops! Something went wrong.<br/><br/>Please click the back button or use the menu to go to a new page.</h2>";
$testdomain = substr_compare ($_GET['xul'],"http://mydomain.com",0,20,FALSE); //make sure the domain name is correct
if ($testdomain == 0) {
$flurl = $_GET['xul'];
} else {
echo $invdmn . " " . $oops;
}
$flurl_headers = #get_headers ($flurl);
if ($flurl_headers[0] == 'HTTP/1.1 404 Not Found') {
echo $filnf . " " . $oops; //Make sure the file exist
} else {
$surl = str_replace (".com/",".com/s/",$flurl);
} //add some characters to url at point to explode
list($url1, $path) = explode ("/s/",$surl); //explode into array of 2 [0]url to domain [1] path
$testpath = substr_compare ($path,"file1/file2/",0,10,FALSE); //make sure the path is correct
if ($testpath == "0") {
$aid = preg_match ("/^[A-Z][a-z]{2}(?:[1-9][0-9]?|1[0-4][0-9]|150):(?:[1-9][0-9]?|1[0-6][0-9]|17[0-6])$/", $_GET['id']);
} else { //make sure the id is valid
echo $pthinv . " " . $oops;
}
if ($aid == 1) {
include($path);
echo "<script type='text/javascript'>";
echo "document.getElementById(x).className = 'vrsehilite';";
echo "document.getElementById(x).scrollIntoView();";
echo "window.scrollBy(0,-100);";
echo "</script>";
} else {
echo $idinv . " " . $oops;
}
?>
Never ever include arbitrary files submitted by the user. Instead, you should only include files from a pre-defined set of your choice. Perhaps something like this:
PHP
$files = array ('file1.php', 'file2.php', 'view.php', 'edit.php');
$id = (int)$_GET['id'];
if (isset ($files[$id])) {
include $files[$id];
} else {
/* Error */
}
Or you could use a regular expression to accept only certain filenames, in this case 1 or more lower case letters followed by 0 or more digits.
$m = array ();
if ( preg_match ('#^http://domain.example/folder1/folder2/([a-z]+[0-9]*\\.php)$#', $m)
&& file_exists ($m[1])) {
include $m[1];
} else {
/* Page not found */
}
You may want to check the return value of include. You may also want to move the folders into the subpattern (...) or use regular expressions for the folder names.
If all you need is to highlight a certain paragraph in a page, you should add a URL fragment that poins to the paragraph's id, and add CSS to style it. Eg:
URL
http://domain.example?id=1#p1
HTML
<p id=p1>This is the target paragraph.
CSS
p:target { /* Style the targeted <p> element */ }
Related
I need to include some js, css type items in specific pages.
So far I've done the following:
define("DIR_ADMIN", "admin");
function fusion($location, $page, $type) {
$dirAdmin = DIRECTORY_SEPARATOR.DIR_ADMIN;
$change = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $location);
$changed = ucfirst($change);
$result = $dirAdmin.$changed;
if($type=='js'){
echo "<script src='$result'></script>\n";
} elseif($type=='css'){
echo "<link rel='stylesheet' href='$result' />";
} elseif($type=='favicon'){
echo "<link rel='shortcut icon' href='$result' />";
} elseif($type=='logo'){
echo "<img src='$result' alt='logo'>";
} else {
echo $result;
}
}
In HTML I called the function like this:
<?php fusion("/assets/vendors/js/vendor.bundle.base.js", "clients", "js"); ?>
First is the file URL, then the page, and finally the file type.
What I'm not able to come up with is the part that receives the value $page.
I created a routing system in php, so all files are accessed like this: index.php?page=clients, except in the case of the homepage which is just index.php.
I need that when the value of $page is set for example with clients it should load all items that have this same when the clients route is accessed.
In some cases the route may have another word attached, for example: index.php?page=clients-add, in which case it should check if there is only the word clients in $_GET.
And if I set the value of $page to all, it should display on all pages. How can I do this?
Within your fusion function you need to pick up which page you're on and then including the displaying of content after that.
Use this snippet to help dictate the page you're on.
$curr_page = "home";
if ( isset( $_GET["page"] ) ) {
$curr_page = $_GET["page"];
}
if ( $page == "all" || $page == $curr_page ) {
//echo items here
}
I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.
I have 3 pages, and they all have this in code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>
If I want "index.php" to echo something that "about.php" does not echo, can you then do it?
As the title says, I'd guess on something like this:
if filename == index.php then
echo=hello
I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.
It's not a perfect solution but as starting point you may use a flag. Beginning like this:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>
And in your index.php include file:
if (helloWorld === TRUE) // or if (!!helloWorld)
echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
echo "do stuff";
}
Newb here working on refining a function to deliver a better user experience.
My goal is that if we are not on the index page (home) of the site, i wish to include a link to return to the home page. But if we are on the index page (Home), i don't wish to display the redundant link in the menu. Below is the function I've built, which you can see here 'school.max-o-matic.com'. at the bottom of the right navigation menu is a link that reads '< back' which should not show on the index page but should ideally show on all other pages.
I'd hoped the use of the exclamation point followed by an equal sign would do the trick but it did not.
<?php
function makeNav($navItem) {
//created variable - plops in what is called when function used on
//page calling the function itself - this is like the strPromp
$output = ''; //Variable of 0 length
foreach ($navItem as $pageLink => $displayedLink) {
if ($pageLink == THIS_PAGE) {
//url matches page - add active class
$output .='<li class="active">'
. '' . $displayedLink . ''
. '</li>' . PHP_EOL;
} //PHP_EOL is php line end for all systems
else {//don't add class
$output .='<li>'
. '' . $displayedLink . ''
. '</li>';
}
}
if ($pageLink != 'index.php') {//add back button to index page
$output .='<li>'
. '< Back'
. '</li>' . PHP_EOL;
} //PHP_EOL is php line end for all systems
$output .='<li>'
. 'contact'
. '</li>';
return $output;
}
?>
if($_SERVER['PHP_SELF'] != '/index.php'){ //process if file is not index file.
}
to exclude access to index file.
if (strpos($_SERVER['PHP_SELF'],'index.php') !== false) { //process if file contains text "index.php" in filename.
}
//to exclude access to any file with name containing "index.php" file
if (basename($_SERVER['SCRIPT_NAME']) != 'index.php'){
//run on all files that ARE NOT index files in any folders.
}
You could check this by using the $_SERVER global variable, which is an array of request information. The key 'SCRIPT_NAME' contains the path to the requested file. The check on this could be:
if (basename($_SERVER['SCRIPT_NAME']) != 'index.php') {
I am trying to add custom html to a 404 page in wordpress.
<?php
$filename = "/find";
if (!file_exists($filename))
echo $filename, " display html2 ";
elseif (!is_dir($filename))
echo $filename, " display html1 ";
?>
If someone visits http://www.demosite.com/about/whatever = Display HTML 1
If someone visits http://www.demosite.com/find/whatever = Display HTML 2
Is this even possible with PHP and HTML?
<?php
$filename = "/find";
if (is_dir($filename)){
echo $filename, " display html1 ";
include('html1.html');
}
else{
echo $filename, " display html2 ";
include('html1.html');
}
?>
This code checks for the folder "find" if it exists it shows html1 else html2.
I guess the better option for you will be this :
Use htaccess when user browse http://yourdomain/about/whatever show the contents from http://yourdomain/page.php?p=about&var=whatever
And get the value of p in your page using $_GET['p'], and show the html accordingly
if($_GET['p'] == "find"){
include('html1.html');
}else{
include('html2.html');
}
I have a simple image-looping script that changes the src of an image.
function cycleNext()
{
++imgIndex;
if(imgIndex>imgCount)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
However, at present, I'm (shudder) manually entering imgCount in my script. The alternative is server-side, but I don't know how to fetch this information. I imagine it's pretty simple, though.
How can I use PHP to supply this script with the number of images in the folder?
<?php
$directory = "Your directory";
$filecount = count(glob("" . $directory . "*.jpg"));
$filecount += count(glob("" . $directory . "*.png"));
?>
Repeat the 2nd line for each extension you wish to count.
function cycleNext()
{
++imgIndex;
if (imgIndex > <?php echo $filecount;?>)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
That should do it.
EDIT:
function cycleNext(imgCount)
{
++imgIndex;
if (imgIndex > imgCount)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
Then when you call cycleNext, call it with the variable.
cycleNext(<?php echo $filecount; ?>);
if the .js file is a separate file. then you can do this:
change the .js for a .php
then you can add <?php ?> tags just like you do in your .php files.
just don't forget to add the header in the code, indicating that the file is a javascript file. like that:
<?php header("Content-type: text/javascript"); ?>
and you will call the file with it's actual name src="file.php"
You can do it in three ways:
Making your .js file a .php file (with the correct mime-type) and just use an echo in that .js.php-file
include the javascript to the <head> tag of your page
echo a variable into a <script> tag in your <head> and use it in your javascript file. Example:
<script type="text/javascript">
var imgCount = <?php echo $imagecount ?>
</script>;
During the generation of the HTML code, simply insert a <script> line, for instance
echo '<script type="text/javascript">';
echo 'var imgCount=' . $NumberOfImages . ';';
echo '</script>';
Just ensure that line is provided before cycleNext() is called (or imgCount is used).