Jump to content

Need help with a special script


nituvious

Recommended Posts

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

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>

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

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

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