trecool999 Posted January 27, 2007 Share Posted January 27, 2007 Ok, after logging in on my site, is there any way that I could replace the old login form at the top with the user's username? Just tell me if you need any scripts because I'm not sure what ones to post... Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 if($username){ print $username;}else{ ?>form here<?} Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Ok, will try that. I take it I would have to rename the Index.html file to Index.php? Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Doesn't work :(. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 Yes, you can only have PHP code on PHP pages.You'll obviously have to adapt the code to your variables. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Doesn't work... still...Even after naming it index.php. I don't think it recognizes '$user' since nothing has been set using it, what could I do to make it recognise it after login? Would it work if I redirected it instantly? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 That was my point, you'll need to adapt it for your code. Once the user is logged in, where are you storing their username? Use that. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Umm... Is there any way you could simplify what you're trying to say?It took me years to learn Javascript, and I'm where I am now with PHP after only 8 hours of starting so I don't know everything... Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Man, this is hopeless, It still doesn't work even after logging in and redirecting :(.Would and include file help with anything? Quote Link to comment Share on other sites More sharing options...
smc Posted January 27, 2007 Share Posted January 27, 2007 Okay basically here is how it brakes down. Take a look at the code:[b]login.php[/b][code]if ( isset($username) ){ //This statement says If the variables $username is set, then print the usernameprint $username; //Print username is another way of saying for PHP to display the text that is stored as the variable $username}else { //Else is saying that if the IF function above didn't apply (ie. $username is NOT set) then carry out these actions::This is where you will place the HTML for your form I recommend using the following formatrequire('/realitivepathofyourloginform.php');}[/code]Now obviously you will have to replace the username variable with whatever you have set on your login script. It is difficult to help you without being able to see the entirety of the script your working with. Everyone has a different style of coding Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Actually, I didn't code this, I followed a tutorial. I'm not experienced enough to code anything more than <?php echo "Bad use of PHP"; ?>. :([quote author=trecool999 link=topic=124327.msg514902#msg514902 date=1169928751]Ok.Login HTML:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>PSPRock - Welcome!</title><style type="text/css"><!--#Layer1 { position:absolute; left:0; top:1px; width:100%; height:180; z-index:1; background-image: url(Images/Site/Backdrop.jpg);}body { background-color: #0099FF;}body,td,th { font-family: Segoe UI; color: #333333;}#Layer2 { position:absolute; left:1px; top:210px; width:224px; height:456px; z-index:2;}#Strip { position:absolute; left:0px; top:182px; width:100%; height:27; z-index:3; background-image: url(Images/Site/StripMiddle.jpg);}#Layer3 { position:absolute; left:0px; top:182px; width:659px; height:27px; z-index:4;}.style1 {font-size: 14px}.style2 {color: #666666}.style3 {color: #333333}--></style></head><body><div id="Layer1"><img src="Images/Site/Logo.jpg" width="700" height="180" /></div><div id="Layer2"></div><div id="Strip"> <div align="right"> <form action="authenticate.php" method="post"> <span class="style1">Username:</span> <input type="text" name="username" size="10"> <span class="style1">Password:</span> <input type="password" name="password" size="10"> <input type="submit" value="Log in" name="submit"> </form></div></div><div class="style1" id="Layer3"><span class="style2"><a href="index.html">Home</a> <span class="style3">-</span> Forums <span class="style3"></span></span>- <span class="style2">Info</span></div></div></body></html>[/code]Login PHP:[code]<?php //Begin PHP file authenticate.php//Retrieve the data the form passed us. All form data passed to PHP//will be in the super global array, $_REQUEST. This is automatically //set for you by PHP (depending on version) You can also use//$_POST or $_GET autoglobals, but for the sake of learning use the one//below$user = $_REQUEST['username'];//get username from form$pass = $_REQUEST['password'];//get password from form//Now strip away an potentially harmful code:$user=strip_tags($user);$pass=strip_tags($pass);//To foil any possible attempts at SQL injection, do the following function//$variable=str_replace("what to look for","what to replace it with",$what_variable_to_use);//Now use the replace function on our variables$user=str_replace(" ","",$user);//remove spaces from username$pass=str_replace(" ","",$pass);//remove spaces from password$user=str_replace("%20","",$user);//remove escaped spaces from username$pass=str_replace("%20","",$pass);//remove escaped spaces from password//And finally, add slashes to escape things like quotes and apostrophes//because they can be used to hijack SQL statements!//use the function, addslashes(), pretty self explanatory$user=addslashes($user);//remove spaces from username$pass=addslashes($pass);//remove spaces from password//First, we need to connect to the server//the format is $connection = mysql_connect("address","username","password");$conn = mysql_connect("localhost","trecool999_user","*password*");//now choose the database to usemysql_select_db("trecool999_user");//Remember how we encrypted the password in step 1? Well we do//the same thing here. We stored the encrypted password//so it couldn't be stolen, but to check what was entered as//the password, we encrypt it, then check it against the //encrypted password in the database. This is pretty standard.//almost EVERY site is going to use a 32 character md5 hash or//an 8 character cipher with a 2 character salt. Don't worry//about what that means :)$pass=md5($pass);//the function md5 creates a unique 32 character string, //no matter what the length of the data you encrypt!//Search for a password AND username match, then return a value//of true if we get any results$request = "SELECT * FROM users WHERE password='".$pass."' AND username='".$user."'";//Pass the request to the mysql connection,//and the data is returned in a blob and//saved in $results$results = mysql_query($request,$conn);//if mysql returns any number of rows great than 0, we know we had a match,//right? Right.if(mysql_num_rows($results))//function returns true if any matches are found{ echo "Hello ".$user.", please wait to be redirected..."; $_SESSION['user'] = $user; $_SESSION['auth'] = true;}else{ echo "Connection failed, please try again."; $_SESSION['auth'] = false;}//End PHP file authenticate.php?>[/code]Register HTML:[code]<form action="signup.php" method="post"> <p>Username: <input type="text" name="username" size="10"> <br> Password: <input type="password" name="password" size="10"> <br> <input type="submit" value="submit" name="submit"> </p> </form>[/code]Register PHP:[code]<?php //Begin PHP file signup.php//Retrieve the data the form passed us. All form data passed to PHP//will be in the super global array, $_REQUEST. This is automatically //set for you by PHP (depending on version) You can also use//$_POST or $_GET autoglobals, but for the sake of learning use the one//below$user = $_REQUEST['username'];//get username from form$pass = $_REQUEST['password'];//get password from form//Now strip away an potentially harmful code:$user=strip_tags($user);$pass=strip_tags($pass);//To foil any possible attempts at SQL injection, do the following function//$variable=str_replace("what to look for","what to replace it with",$what_variable_to_use);//Now use the replace function on our variables$user=str_replace(" ","",$user);//remove spaces from username$pass=str_replace(" ","",$pass);//remove spaces from password$user=str_replace("%20","",$user);//remove escaped spaces from username$pass=str_replace("%20","",$pass);//remove escaped spaces from password//And finally, add slashes to escape things like quotes and apostrophes//because they can be used to hijack SQL statements!//use the function, addslashes(), pretty self explanatory$user=addslashes($user);//remove spaces from username$pass=addslashes($pass);//remove spaces from password//Now, after all that replacing, see if the password or username less than the required length//Note that it is good to require a minimum pass/user length to provide greater security$min_len = 6;if(strlen($user) < $min_len || strlen($pass) < $min_len){die("User/password was not long enough!");//Kick us out of PHP}//First, we need to connect to the server//the format is $connection = mysql_connect("address","username","password");$conn = mysql_connect("localhost","trecool999_user","*password*");//now choose the database to usemysql_select_db("trecool999_user");//encrypt the users password so it cannot be retrieved by anyone!$pass=md5($pass);//the function md5 creates a unique 32 character string, //no matter what the length of the data you encrypt!//Save the request in SQL syntax to a string$request = "INSERT INTO users values(0,'".$user."','".$pass."')";//Pass the request to the mysql connection,//and the data is returned in a blob and//saved in $results$results = mysql_query($request,$conn);if($results){ echo "User account created";}else{ echo "There was an error. The user account was not created.";}//End PHP file signup.php?>[/code]Someone help quickly! I only have 1 hour and 43 minutes til' I have to go to bed! 13 isn't the best age for going at late times :-[.[/quote] Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Bump.PLEASE HELP!I gotta' go to bed soon:(:(:(. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 No one is going to do it for you.Save the username in the session so you can use it on later pages. Quote Link to comment Share on other sites More sharing options...
Cagecrawler Posted January 27, 2007 Share Posted January 27, 2007 [code]<?phpif($_SESSION['user']){echo $_SESSION['user'];}else{//INSERT FORM HERE}?>[/code] Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Yes, but how??? :'( You're telling me all these things about sessions when all I know is how to open and close them :(. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 When the user logs in, do $_SESSION['username'] = $username; <-- whatever the hell your username variable is, don't just copy and paste.Then use the code cage posted.Put session_start() on the top of every page. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 So where do I put the $_SESSION thing and on what page if the login is on the index page? I've got everything else sorted out except that... Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2007 Share Posted January 27, 2007 Where you process the login. Quote Link to comment Share on other sites More sharing options...
AXiSS Posted January 27, 2007 Share Posted January 27, 2007 I don't know your exact situation, but I don't think you need this finished in one night. So I'd suggest getting a book on PHP or try some intelligent form of learning to understand what PHP is and how it works. I've been working with PHP for over a month now and only today did I accomplish what you are asking to learn in a few hours. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted January 27, 2007 Share Posted January 27, 2007 In every single script you have using your website use[code]<?phpsession_start();?>[/code]Then, use something like this....Login.php[code]<form method="post" action="login.php"><table width="98%" align="center"><? if (isset($HTTP_POST_VARS['user_name']) && isset($HTTP_POST_VARS['user_password'])){$user_name = $HTTP_POST_VARS['user_name'];$user_password = $HTTP_POST_VARS['user_password'];$query = 'SELECT * FROM table ' ."where user_name='$user_name' "." and user_password='$user_password'";$result = mysql_query($query);if (mysql_num_rows($result) >0 ){$HTTP_SESSION_VARS['valid_user'] = $user_name;} }if (isset($HTTP_SESSION_VARS['valid_user'])){Echo '<table width="50%" align="center"><tr><td width="615" height="21" valign="top" align="center">Welcome '.$HTTP_SESSION_VARS['valid_user'].'</td></tr></table>';mysql_query("UPDATE table SET user_loggedin=user_loggedin+1 WHERE user_name = '".$HTTP_SESSION_VARS['valid_user']."'");} else{if (isset($user_name)){ Echo("<td height=\"22\" align=\"center\" colspan=\"2\" valign=\"middle\" style=\"BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B\"> <strong>You Could Not Be Logged In</strong></td>"); }?><tr> <td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22" valign="middle"> <strong>Username:</strong></td><td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22"><input class="temptable1" type="text" name="user_name"></td></tr><tr> <td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22" valign="middle"> <strong>Password:</strong></td><td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22"><input type="password" name="user_password"></td></tr><tr> <td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22" valign="middle"> <strong><a href="forgot_password.php">Forgot Password?</a></strong></td><td style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #181D2B" width="49%" height="22"><input class="forminput2" type="submit" value="Login"></td><tr> </table></form> <? } ?>[/code]That Login.php creates a session for the user called 'valid_user'Then to do what you ask to change the form to it displaying the persons username, use something along the lines of.[code]<?phpif (isset($HTTP_SESSION_VARS['valid_user'])){?><?phpecho $user_name;?><?}else{?><div align="right"> <form action="authenticate.php" method="post"> <span class="style1">Username:</span> <input type="text" name="username" size="10"> <span class="style1">Password:</span> <input type="password" name="password" size="10"> <input type="submit" value="Log in" name="submit"> </form></div><?php} ?>[/code]This is a login and session script i used for one of my websites over a year ago and i have just copy and pasted it from my scripts and modified it a little. You may have to modify it youself to fit in with your website and work with the script to make it work ( if it doesnt ), but it should because it did for me.these are only guidelines. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Wouldn't that create a whole new login system? Besides, I don't have a month to create this, that's why. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 [quote author=jesirose link=topic=124331.msg514980#msg514980 date=1169936878]Where you process the login.[/quote]I just checked through 'authenticate.php' and it seems it alreayd has '$_SESSION['user'] = $user;'.I guessall I need to do now is start the session on each page right? Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 Ok, I have put the 'S_SESSION' thing in after login is processed and I have put 'session_start()'in <head>. It doesn't work so I'm wondering if 'session_start()' should be in body instead? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 27, 2007 Share Posted January 27, 2007 First you need to find yourself some tutorials because you obviously don't have enough PHP knowledge to do what your trying to do. So go to the tutorial section of phpfreaks and read about sessions rather then depending on everyone else to do it for you before you even attemt to figure it out. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted January 27, 2007 Author Share Posted January 27, 2007 [quote author=pocobueno1388 link=topic=124331.msg515002#msg515002 date=1169939355]First you need to find yourself some tutorials because you obviously don't have enough PHP knowledge to do what your trying to do. So go to the tutorial section of phpfreaks and read about sessions rather then depending on everyone else to do it for you before you even attemt to figure it out.[/quote]So 3 hours of google searching on how to create a login, register and MySQL database isn't long enough? My eyes hurt already. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.