0

Been looking in stackoverflow for the answer or a hint to it but have not found what I am looking for, seems most of it is about sql stuff. Anyway my website is using Dynamic linking so the main index has all the html stuff and then the content box is the only thing that changed based on the php files I have in my pages folder.

I can access the php pages just fine but what I am trying to do is access php pages from within a directory inside the pages folder.

Most pages are linked using this which is just the about.php within the pages folder index.php?page=about

but how would I link other directories inside this pages folder? lets say I have the directory contact which has index.php inside it so /contact/index.php how would I link to that one?

This is the code I have so far for my content area of my site, a friend wrote it for me years ago and I can not contact him to get help now.

<?php
function open_page($string){
    if (preg_match("/^[a-zA-Z]+$/", $string)){
        $string = $string;
    }else{ 
        $string = "";
    }
    return $string;
}
$page = open_page($_GET['page']);
if ($page){
    $file = "pages/".$page.".php";
    if (file_exists($file)){
        $pagename = $page;
    }else{
        $pagename = "404";
    }
    include("pages/".$pagename.".php");
}else{
    echo 'ADD CONTENT HERE';
}
?>

3 Answers 3

1

While creating the link, you can use

index.php?page=<?php echo urlencode('contact/index'); ?>

and in your code replace

$page = urldecode($_GET['page']);
Sign up to request clarification or add additional context in comments.

2 Comments

Your code snippet works however problem is I have a form on the page and that links to a variable and when clicking the submit button it gives me a 404 error. <form action="<?php echo $URL?>" method="post" class="Form">
that's because your forms method is post. either change it to 'get' or
0

The way it's coded I don't think you can actually access the file through:

index.php?page=contact/index

Because the expression is not matching: "/". You can modify the expression to add it though, it's relatively simple.

preg_match("/^[a-zA-Z\/]+$/", $string)

I'm not sure about its security implications though, i feel like there shouldn't be a problem, but there's just something about it that makes me feel uneasy. I would consider adding special rules for the subfolders and/or restructure the code around the glob() function which looks more safe.

3 Comments

Problem is I have a form on the page and that links to a variable and when clicking the submit button it gives me a 404 error. <form action="<?php echo $URL?>" method="post" class="Form">
What if you put it like: <form action="index.php" method="post" class="Form"> <input type="hidden" value="contact/index" name="page"> </form> ¿Does index.php?page=contact/index works through the browser?
Nope it redirects the page to a 404 error, To be honest its for a donation system I am setting up on my page so the directory is "donations" and the php file is donate.php, however when clicking the paypal button it should link to paypal but instead throws a 404
0

Glad to help here. First of all, I am assuming you know your directory structure before hand if you know where a link "contact/*" needs to sit on your page. If not, then I guess your just reading the directory and returning all available links in a list on the page (perhaps for a menu).

So, if I have understood you correctly and really, to give you a hint rather than a full answer as I cannot be expected to write out the exact program without knowing your final intention I would change the following:

if (file_exists($file)){
    $pagename = $page; .....

to

if (file_exists($file)){
    $pagename = $page;
    // $page exists. I will assume you have a folder by the same name in this case
    $dir = scandir(__FILE__ . "/" . $page . "/"); // just double check I have the direcotry path correct. this should return array with the '..' & '.' locations plus other scripts. it is not recursive
    //scan through the $dir var printing out the relevant html anchor links, for example
    echo "<a href='" . $page . "/" . $dir[position_from_your_loop_as_you_scan] . "'></a>";
    //or attach the above to a variable for ouputting to page/console later

the above is just rough code but I hope you get the idea and have enough to go on.

alternatively, check out glob() function of php.


Btw, you are using dynamic includes. Although in your case it does not seem to matter too much it is still good practice to sanitise your inputs. I would swap out

$page = open_page($_GET['page']);

for something like

$page = open_page(filter_var($_GET['page'], **FILTER_SANITIZE_CONSTANT**));

Look up here http://php.net/manual/en/filter.filters.sanitize.php for the available filter constants you use.

It is also good practise to check for the existence of the $_GET variable you are after (in this case, 'page') with the php isset() method and then do else if not existing or valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.