nituvious Posted July 30, 2010 Share Posted July 30, 2010 Hi guys, I am writing a "login" script that will be on my main page(I have it inside of <head></head> tags). I want it to take a user name and password, store it in a variable that is persistant with all pages on my website. Is that possible? How can I make a variable that will carry over to a page that doesn't have the original code? E,g, $Login = "alogin"; then when my client goes to a different page $Login still == "alogin"? I hope this is understandable. I am not writing a very advanced script, I just want the input form to store the users login name/password so he or she may access a section of the site that has more options available if they're logged in. Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/ Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 You should post the code you have so far. Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1092875 Share on other sites More sharing options...
nituvious Posted July 30, 2010 Author Share Posted July 30, 2010 Here are my scripts. I have Login.php inside of my index.php, it contains Login.php inside of <head></head> tags. I know my php code looks awful and is probably done entirely wrong, but I am new to php and just did this to learn it. I also thought it would be neat to have a login that displayed extra options on pages so I decided to try my hand at this. Login.php <?php $Username = "test1"; $Password = "test2"; $Login = $_POST['login']; $Pass = $_POST['password']; if ($Login != $Username && $Pass != $Password) { ?> <!-- Login page goes here --> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" value="login" name="login" class="login"/> <input type="password" value="password" name="password" class="password"/> <input type="submit" value=" " name="submit" class="submit"/> </form> <?PHP } else { ?> <!-- Redirect to a page to set cookies, but I don't have any way to get $Login and $Pass to this page.. --> <script type="text/javascript"> window.location = "scripts/logging_in.php" </script> <?PHP } ?> Logging_In.php <?php setcookie("login",$Login,time()+7200); setcookie("password",$Password,time()+7200); ?> <script type="text/javascript"> window.location = "http://MyPage.com/" </script> Some of Index.php <head> <?php if (isset($_COOKIE["login"]) && isset($_COOKIE["password"])) { $login = $_COOKIE["login"]; $pass = $_COOKIE["password"]; if ($login != $Username && $pass != $Password) { include("scripts/login.php"); } else { // do stuff echo "Welcome!"; } } else { include("scripts/login.php"); } ?> </head> Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093056 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 Is there any reason to include login.php into the head section ? It should be in-between opening and closing body tags <body> </body> Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093069 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Share Posted July 30, 2010 If I understand correctly, you can just set your form action to your script, like so: <form method="post" action="scripts/logging_in.php"> Then on scripts/logging_in.php you can catch the variables like this: // Remember to use mysql_real_escape_string() if you are using MySQL // I removed spaces by using trim() $username = trim($_POST['login']); $password = trim($_POST['password']); This is better too, because what if your visitor has JavaScript disabled? Then your redirect won't work. And I want it to take a user name and password, store it in a variable that is persistant with all pages on my website. Is that possible? How can I make a variable that will carry over to a page that doesn't have the original code? E,g, $Login = "alogin"; then when my client goes to a different page $Login still == "alogin"? That would be a session. You can make a session that is accessible on all pages like this: <?php session_start(); // This MUST be the FIRST thing on your pages. Do this on all the pages where you want to use sessions $_SESSION['username'] = "some value"; Then you can access $_SESSION['username'] on all of your pages. However, I would not recommend using sessions for this; it would be better to solve your problem with the form action. But a session is good to check if a user is logged in or not. Hope that made sense. EDIT: Also, you should start your variables with a lowercase letter and any other words with an uppercase letter. I don't remember what this is naming convention is called, but here are a few examples: $user $pass $someVariable $someLongerVariable Just a heads up. Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093093 Share on other sites More sharing options...
nituvious Posted July 30, 2010 Author Share Posted July 30, 2010 Oh wow, thank you! Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093111 Share on other sites More sharing options...
TOA Posted July 30, 2010 Share Posted July 30, 2010 Also, you should start your variables with a lowercase letter and any other words with an uppercase letter. I don't remember what this is naming convention is called, but here are a few examples: Camel-casing Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093132 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Share Posted July 30, 2010 Camel-casing Ah yeah, that's right. I remembered that it was something strange. Thanks. Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093163 Share on other sites More sharing options...
TOA Posted July 30, 2010 Share Posted July 30, 2010 Camel-casing Ah yeah, that's right. I remembered that it was something strange. Thanks. It is isn't it Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1093164 Share on other sites More sharing options...
nituvious Posted August 13, 2010 Author Share Posted August 13, 2010 Camel-casing Ah yeah, that's right. I remembered that it was something strange. Thanks. It is isn't it I always thought camel casing was: CamelCasing not: camelCasing Link to comment https://forums.phpfreaks.com/topic/209296-need-help-with-a-special-script/#findComment-1098911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.