Jump to content

DexterTheCat

New Members
  • Posts

    9
  • Joined

  • Last visited

DexterTheCat's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. MySQL is my own summer project when the university don't have any courses in MySQL or databases. Thank for your helpfull adivce and tips.
  2. I am not silly but I was told to do it this way! I don't really get how a university course can teach php but not let the student use MySQL...
  3. Yeah! I agree but I must make this one without...
  4. Sounds awesome ginerjm! I will try that out. When I put my post to file now with a+ does it fill the file from the last post. Is there a way ro reverse so the most present post comes first?
  5. I got an idea with my homepage in progress. It's a blog and I want to sort my post in a navigation bar. Is it possible to do a script with $date function to create a new file the 1th of every month. Then use file_get_content on the current month. To clarify in this stage I haven't started connecting my database.
  6. I continue to write in this post becouse I have trouble to write my users to txt-file. I understand the session with users and password and what it does and how. But write to file, specially write varibles to files has been a hard nut to crack. I am really stuck and can't see what I have done wrong. Any tips or advice to get forward? <?php session_start(); $message = ''; $users = array( 'Dexter' => 'Meow1', 'Garfield' => 'Meow2', 'Meowington' => 'Meow3' ); if($_SERVER['REQUEST_METHOD'] == 'POST') { if(isset($users[$_POST['username']]) && $users[$_POST['username']] == $_POST['password']) { //A session that keep the user logged in during the visit to secure_area.php $_SESSION['login'] = 1; $_SESSION['username'] = $_POST['username']; header('Location: secured_area.php'); exit; } else { //Using the varible $message from earlier if wrong username or password $message = 'Wrong usename or password'; } //Write user and password to file and the path for the .txt file $saveusers = $users ."\n"; $text = fopen('users.txt', 'a+'); $path = 'users.txt'; if(file_exists($path)) { $members = file_get_contents($path); $members = explode("\n", $members); $multiusers = array(); foreach($members as $value); { $userone = explode(',', $value); $multiusers[$userone[0]] = $userone[1]; } if(!empty($users) && fwrite($text, $saveusers) && ! isset($multiusers[$_POST['user']])) { echo "Do you have a user here?"; } } else{ session_write_close(); } fclose ($text); $path = 'users'; } ?> <?php $page_title = 'Loggin'; include('C:/wamp/www/PHP/includes/header.html'); ?> <?php echo "<h2>Logga in</h2>"; echo "<br />"; //User form in html below ?> <form action="" method="post"> <fieldset><legend>Your username and passwordr</legend> <tr> <td colspan="2"></td> </tr> <tr> <td>Username:</td><br /><td><input type="text" name="username" /></td> </tr> <tr><br /> <td>Password:</td><br /><td><input type="password" name="password" /></td> </tr><br /> <tr><br /> <td> </td><td><input type="submit" value="Login" /></td> <td>Save password</td><input type="checkbox" name="saveBox" /> </tr><?php echo $message; ?> </fieldset> </form> <?php / include('C:/wamp/www/PHP/includes/footer.html'); ?>
  7. Thanks Frank_b! Now I understand what I did wrong and how this works!
  8. If I fix the arrays don't I keep the users at the login page?
  9. Hi! I have read like crazy to find a tutorial on a login page without My_SQL. Anyway I am working on a easy login/logged out page with sessions. Here is the login page with tree users in an array. The things that I need some hints to solve is, when clicking on login the error message don't show. Instead the script goes to the logged in page right away. And when you write the wrong password you get loged in anyway. I am not sure how or if it's possible to write a varible to a file this way. But I tried and recived a parse error with the txt varible. When searching for topics I get more confused with the My_SQL varibles. I am near a breaking point at cracking the first step on PHP, but need some advice. <?php $page_title = 'Logged in'; //Dynamic title include('C:/wamp/www/PHP/includes/header.html'); ?> <?php session_start(); //A array for the sites users with passwords $users = array( 'Dexter'=>'meow1', 'Garfield'=>'meow2', 'Miro'=>'meow3' ); //A handle to save the varible users to file on a new line from the last entry $handle = fopen("newusers.txt, \n\r") $txt = $users; fclose($handle); if(isset($_GET['logout'])) { $_SESSION['username'] = ''; header('Location: ' . $_SERVER['PHP_SELF']); } if(isset($_POST['username'])) { if($users[$_POST['username']] == $_POST['password']) { $_SESSION['username'] = $_POST['username']; }else { echo "Something went wrong, Please try again"; } } ?> <?php echo "<h3>Login</h3>"; echo "<br />"; ?> <!--A legend form to login--> <fieldset><legend>Fill in your username and password</legend> <form name="login" action="777log.php" method="post"> Username: <br /> <input type="text" name="username" value="" /><br /> Password: <br /> <input type="password" name="password" value="" /><br /> <br /> <input type="submit" name="submit" value="Login" /> </fieldset> </form> <?php //Footer include file include('C:/wamp/www/PHP/includes/footer.html'); ?> The logged in page <?php //Header $page_title = 'Reading a file'; include('C:/wamp/www/PHP/includes/header.html'); ?> <?php session_start(); //Use an array forthe sites users $users = array( 'Dexter'=>'meow1', 'Garfield'=>'meow2', 'Miro'=>'meow3' ); // if(isset($_GET['logout'])) { $_SESSION['username'] = ''; echo "You are now loged out"; //The user is loged out and returned to the login page header('Location: ' . $_SERVER['PHP_SELF']); } if(isset($_POST['username'])) { //Something goes wrong here when login without any boxes filled if($users[$_POST['username']] == $_POST['password']) { $_SESSION['username'] = $_POST['username']; }else { echo "Something went wrong, Please try again"; $redirect = "Location: 777.php"; } } ?> <?php if($_SESSION['username']): ?> <p><h2>Welcome <?=$_SESSION['username']?></h2></p> <p align="right"><a href="777.php">Logga ut</a></p><?php endif; ?> <p>Today Ben&Jerrys Chunky Monkey is my favorite!</p> <?php //Footer include('C:/wamp/www/PHP/includes/footer.html'); ?>
×
×
  • Create New...

Important Information

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.