Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

A Brainstorming Question...


10 replies to this topic

#1 anwiii

    I wont bite...unless you WANT me too

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,704 posts
  • Gender:Male
  • Location:Chilhowee, MO
  • Interests:watching grass grow....
  • myCENT:62.06
  • Spam Patrol

Posted 11 April 2010 - 07:17 AM

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....

#2 rvalkass

    apt-get moo

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3,107 posts
  • Gender:Male
  • Location:Devon, England
  • Interests:At the moment, Physics mainly!
  • myCENT:69.42
  • Spam Patrol

Posted 11 April 2010 - 08:29 AM

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 www.example.org and the user types in directory then combine those two with JavaScript and forward the user's browser to www.example.org/directory. 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.

#3 nirvaman

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 210 posts
  • Gender:Male
  • Location:Localhost
  • myCENT:12.94

Posted 11 April 2010 - 10:42 AM

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://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
</head>
<body>
<form 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.

#4 web_designer

    "french rose sparkle under moonlight"...do you believe in the magic of moonlight??!!...

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,385 posts
  • Gender:Female
  • Location:US, CA
  • Interests:internet and the web
    reading books
    sport
    watching tv series
    drawings and art
  • myCENT:12.10
  • Spam Patrol

Posted 11 April 2010 - 02:32 PM

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";
}
   
?>


#5 web_designer

    "french rose sparkle under moonlight"...do you believe in the magic of moonlight??!!...

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,385 posts
  • Gender:Female
  • Location:US, CA
  • Interests:internet and the web
    reading books
    sport
    watching tv series
    drawings and art
  • myCENT:12.10
  • Spam Patrol

Posted 11 April 2010 - 05:23 PM

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

Quote

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.

#6 truefusion

    Coincidence is non-sequitur, therefore everything has a reason for its existence (except if they are eternal).

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3,216 posts
  • Gender:Male
  • Location:No, not there. Not there either. Yes, you'll never figure it out.
  • Interests:God, Christianity.
  • myCENT:86.16

Posted 11 April 2010 - 05:27 PM

View Postweb_designer, on Apr 11 2010, 10:32 AM, said:

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 slash
if ($dir_name[(strlen($dir_name)-1)] == "/")
		$dir_name[(strlen($dir_name)-1)] = "";

// If location exists and is a directory
if (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 site
else
{
		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.

#7 nirvaman

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 210 posts
  • Gender:Male
  • Location:Localhost
  • myCENT:12.94

Posted 11 April 2010 - 09:13 PM

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 .

#8 Baniboy

    Advocatus Diaboli

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 878 posts
  • Gender:Male
  • Location:/root
  • Interests:Everything...
  • myCENT:37.47

Posted 29 April 2010 - 07:27 PM

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, 29 April 2010 - 07:33 PM.


#9 truefusion

    Coincidence is non-sequitur, therefore everything has a reason for its existence (except if they are eternal).

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3,216 posts
  • Gender:Male
  • Location:No, not there. Not there either. Yes, you'll never figure it out.
  • Interests:God, Christianity.
  • myCENT:86.16

Posted 29 April 2010 - 09:23 PM

View PostBaniboy, on Apr 29 2010, 03:27 PM, said:

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).

#10 xpress

    XPRESSing the XPRESSion

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 709 posts
  • Gender:Male
  • Location:X Universe
  • Interests:Computer Security
    Java and PHP
    Music
  • myCENT:93.53
  • Spam Patrol

Posted 03 May 2010 - 06:22 AM

View Postweb_designer, on Apr 11 2010, 10:53 PM, said:

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




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users