Jump to content
xisto Community
Sign in to follow this  
anwiii

A Brainstorming Question...

Recommended Posts

while i was trying to think of some ideas for a new site, i came across a question in my head.how would one go about creating an input box on a web page where a user would enter a directory name, hit enter, and would automatically take you to that specific directory page. also, if the directory doesn't exist, it would take you back to the same page except with a message saying that that directory doesn't exist.by directory, i mean a folder in the public_html area.so before i think more on how i would implement my brain storming idea, i would like to know how it can be done first and go from there and see if it's practical or not for what i have in mind....

Share this post


Link to post
Share on other sites

Use JavaScript to take whatever they type in, and append it to your URL. Then forward the browser to that address. So, if your URL is http://www.example.org/ and the user types in directory then combine those two with JavaScript and forward the user's browser to http://forums.xisto.com/no_longer_exists/. I'm not sure how you would check the existence of the directory with JavScript however.

PHP is also an option, but I'm not sure exactly how that would work with your requirement for 'automatically' taking them to the directory. The same logic applies to taking the input from the user, sanitising it, and adding it to the URL. To check the directory exists use the PHP function file_exists.



Share this post


Link to post
Share on other sites

Hi anwiii
I think you can do this using PHP

Here is the code for your main page n where you have the form to enter the desired directory

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"	"http://forums.xisto.com/no_longer_exists/ xmlns="http://forums.xisto.com/no_longer_exists/ method=post action="red.php">				<p>				<table><tr><td>Directory:</td><td><input type="text" name="dir_name" class="item" value=""/></td></tr><tr><td colspan='2'>				<input type="Submit" name="submit" class="submit" value="submit" tabindex="3" />				</td></tr></table>				</p></form></body></html>

Then you need a page that redirects your members to the directory they want , and let's call it red.php
here is the code for red.php

<?php$dir_name = $_REQUEST['dir_name']?><?php 	header('Location: http://freedomaindot.com/'.$dir_name.'');?>

The member will be redirected to the directory under the domain name after the / .

PS: remember to change "freedomaindot.com" to your domain name.

Share this post


Link to post
Share on other sites

perfect solution nirvaman, but i will add this to your php file, to check if the folder is exist or not

<?php$dir_name = $_REQUEST['dir_name'];if (is_dir($dir_name)) {header('Location: http://freedomaindot.com/'.$dir_name.'');}else {echo "Directory not found";}   ?>

Share this post


Link to post
Share on other sites

great addition truefusion, and about the redirection i wanted to redirect him to the server again but i thin he want's an error message to appear

also, if the directory doesn't exist, it would take you back to the same page except with a message saying that that directory doesn't exist.

since, it not possible to send an error message buy echo and a redirect header in the same time, because 100% we will get an error message, so i chosen the error message instead of redirection to show the visitor that there is an error happens.

Share this post


Link to post
Share on other sites

perfect solution nirvaman, but i will add this to your php file, to check if the folder is exist or not

And to add on to your code, let us add a redirect to go back to the main site:

 

<?php// $_REQUEST allows for both $_POST and $_GET methods to work$dir_name = str_replace("\n", "", $_REQUEST['dir_name']);// Strip any ending slashif ($dir_name[(strlen($dir_name)-1)] == "/")		$dir_name[(strlen($dir_name)-1)] = "";// If location exists and is a directoryif (is_dir($dir_name)){		// Redirect the user to the directory		header('Location: http://'. $_SERVER['SERVER_NAME'] .'/'. $dir_name .'/');}// Else send them back to the main siteelse{		header('Location: http://'. $_SERVER['SERVER_NAME'] .'/');}   ?>
anwiii, i know you want it to go back in history one level, but there is no 100% sure way to make that happen. Also, you may want to inform the user that the directory could not be found and so was redirected (to avoid confusion). Also note that since the header function is used, it must come before any output to the browser. Also note that this will only work with directories one level below public_html unless the user enters "dir1/dir2" as the directory path. And concerning security, i am unaware of any security flaws.

Share this post


Link to post
Share on other sites

Good addition webdesigner and truefusion , i didn't thought about it .And for the error pages when directory don't exist i think he can make a redirection instead of the 404 error page .

Share this post


Link to post
Share on other sites

There is a way you could do both. Stay on the "error page", but with a JavaScript button/link to go one page back in history. So the user clicks that if he/she wants to go back after seeing the "directory not found" message.

<a href="#" onClick="history.go(-1)">Go Back</a>

BTW, I'm not very good at this so I'm probably wrong, but the part of truefusion's code where it removes the slash, shouldn't the if have "{}" or does it work like that as well?

And I also realized another way. If the form would 'action' to the current page itself, and then the contents would be checked with truefusion's code and then if everything is okay, a redirect to the appropriate directory and if not, a little box on the same page saying the directory wasn't found.
Edited by Baniboy (see edit history)

Share this post


Link to post
Share on other sites

BTW, I'm not very good at this so I'm probably wrong, but the part of truefusion's code where it removes the slash, shouldn't the if have "{}" or does it work like that as well?

When there are no curly brackets, PHP will only consider the one statement following the if, foreach, for, or similar statements. This follows from C, which PHP is coded in. Conditional statements, therefore, can be written like
if ($this) do_this(); else do_that();if ($this) do_this();else do_that();
This may or may not make things easier to follow, but it does look more like English (though other languages come closer do to their "simpler" syntax).

Share this post


Link to post
Share on other sites

great addition truefusion, and about the redirection i wanted to redirect him to the server again but i thin he want's an error message to appear
since, it not possible to send an error message buy echo and a redirect header in the same time, because 100% we will get an error message, so i chosen the error message instead of redirection to show the visitor that there is an error happens.


Well, if you want to show error message, and want to redirect the user to another page, it is possible.

Just use header refresh, which will redirects the user after certain time. I am just changing truefusion's code to show you that.

if (is_dir($dir_name)){		// Redirect the user to the directory		header('Location: http://'. $_SERVER['SERVER_NAME'] .'/'. $dir_name .'/');}// This else, will display the error, and then redirects the user as specified.else{		echo "Directory Not Found";		echo "<br/>You'll be redirected in 3 seconds";		header('Refresh:3, http://'. $_SERVER['SERVER_NAME'] .'/');}

The header refresh of php refreshes the above page in 3 seconds, so the visitor will see the error msg and will be redirected.

Anyway AJAX is the best solution for his brainstorming ;) question

Share this post


Link to post
Share on other sites

Hi!@anwiiThere are a couple of different approaches that you could try. You can use a client-side approach and create a Javascript that makes an AJAX request to determine if a particular page exists on the server. The request is make to retrieve the directory, which basically means that the default document within that directory is retrieved. If it gets back an error response, we assume that the directory does not exist and can display a message to the user, again via Javascript. The advantage of this approach is that the page does not refresh. We can accomplish something similar using inline frames - the user would see an error within the inline frame while the textbox for entering the directory is still visible... it is a crude approach, but it gets the job done with AJAX.From the server-side, you could implement a solution with any programming language of choice, such as PHP, Java, or ASP.NET. What you are trying to accomplish is pretty much what many MVC frameworks do - when you type in a directory name in the URL bar, the server-side script that makes up the MVC framework checks to see if it can find a controller and view to match and then either displays the page or goes BEEEP... wrong answer! You can specify a custom error page to say Page Not Found or something similar. However, I'm not really sure if you are trying to create your own web development framework or are simply developing something to enable visitors to browse through the site, so if you can provide more details about what you are trying to accomplish, that would really help us in telling you the exact course that you need to follow to achieve your end result.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.