www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript - Passing data between html pages

Hybrid View

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    javascript - Passing data between html pages

    Hello guys! This is my 1 thread here. I'm' sorry my english ,but lets go ..i need your help!

    I have 2 pages HTML , with javascript login (without database) .
    Works with text file where username and password = txt file name ( leo123.txt)

    I need get the username (when someone enter username) and pass data to next page.


    Example : username : leo (Login.html) >>>>>>> (Main.html) Label Username = Leo
    If username in Main.html page <> leo close this page


    This is my code LOGIN.HTML

    <script>
    function click() {
    if (event.button==2||event.button==3) {
    oncontextmenu='return false';
    }
    }
    document.onmousedown=click
    document.oncontextmenu = new Function("return false;")
    </script>


    <script type="text/javascript">
    <!--
    function authPass(u,p) {
    if (u && p) self.location = "Auth.html?" + u + p;
    return false;
    }
    // -->
    </script>

    <div>
    <script type="text/javascript">
    <!--

    if (location.search.indexOf("auth=failed") != -1) {

    document.write("<span style=\"color:#cad439\">Error! Check your username or password</span>");
    }

    -->

    </script>

    HTML Code:
    <form id="form1" action="javascript://" onsubmit="return authPass(this.user.value,this.pass.value)">
    				
                   <table>
    			   <tr>
    			     <td>Login box</td><td></td></tr>
    				<tr>
    				    
    					<td>Username: </td><td><input type="text" name="user" /></td>
    				</tr>
    				<tr>
    					<td>Password: </td><td><input type="password" name="pass" /></td>
    				</tr>
    				<tr>
    					<td colspan="4" align="left"><input type="submit" value="Login" /><input name="Lembrar" type="button" onclick="window.open('https://Main.html')" value="Lost your username?" /></td>
    				</tr>
    			</table>
    
    		</form>


    AUTH.HTML ( CHECK OUR LOGIN )


    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>

    <script type="text/javascript">
    <!--

    var authstring = "<script type=\"text/javascript\" src=\"" + location.search.substring(1) + ".txt\"><\/script>";
    if (location.search) document.write(authstring);

    // -->
    </script>
    </head>
    <body>
    <script type="text/javascript">
    <!--
    window.auth ? location.replace(redirect) : location.replace("index.html?auth=failed");
    // -->
    </script>
    </body>
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    using php ($_SESSION) + mysql will extremely reduce the headache
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    899
    I can't help but point out how insecure this is...
    And just a headsup, I see you are attempting to block right-clicks on the first page but these kinds of scripts are currently pointless. Years ago they served a purpose in preventing people from viewing source code and potentially getting data (or files) they shouldn't be getting. Then browsers starting adding developer tools (F12) that allow you to view the source, inject javascript (via console) and essentially do whatever you want with the page (on your own local copy of course).

    Moving on, you really should move towards something like $_SESSION like Padonak mentioned. But in the event you don't want to use a database or don't have PHP installed you could always use javascripts localStorage and sessionStorage (or cookies if you care about IE7). This would allow you to store the username and password, validate it and remember the username on all pages of your site.

    Currently with what you have there isn't really a proper way to get the username. You are mashing the username and password together at the end of the url (eg. Auth.html?leo123) and because of this there is no way to know where the username ends and where the password begins. You could separate the strings with a dilimmeter (eg. Auth.html?leo_123) which would allow you to use .split("_") on the location.search string so you could tell the difference between the two.

    But even that is one of the worst ways to do something like this. So even if you don't want to use PHP's $_SESSION, MySQL or javascripts localStorage and sessionStorage you should pass the username and password into the url as proper arguments, not one mashed up string (eg. Auth.html?u=leo&pass=123). This does mean you will have to do more work on your Auth page because you then have to read these arguments, but ultimately the data will be better organized.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles