Link to home
Start Free TrialLog in
Avatar of qqchen
qqchen

asked on

PHP echo problem, plz help!

i am creating a login page, and i want it do if user input incorrect credentials, a message "in correct username/password" is displayed on the login page(without using a message box or alert). i tried darron_chapman's code(https://www.experts-exchange.com/questions/23156476/use-innerHTML-in-a-echo-statement-in-a-php-file.html) but it seems doesn't work on mine. I got Undefined variable error!  anyone can help me with it?
<html>
	<head>		
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
		<title>Login</title>
	</head>
	<body>
		<form method="POST" action="log.php">
			<fieldset>
			<legend>Login</legend>
				UserName:
				<input name="UserName" type="text" size="10"  tabindex="1" style="font-size:12px;" />
            <br />                                       
				Password:
				<input name="Password" type="password" size="10"  tabindex="1" style="font-size:12px;" />	
			<br />
			<span id="error"><?php echo $message;?></span>
			<br /><input type="submit" value="Submit" name="submit" class="content"/><br />
		  </fieldset>
		</form>
	</body>
</html>

Open in new window

<?php
   session_start();
  
   if($_REQUEST['UserName'] == "wang" && $_REQUEST['Password'] == "1234"){
      $_SESSION['UserName'] = "wang";
      $_SESSION['Password'] = "1234";
      header("Location: index.php");
   }
   else{
      header("Location: login.php");
	  $message="Incorrect username/password";
   }
?>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

You're trying to share a variable between page, it's not possible. You may use a variable session here again.


<?php
   session_start();
  
   if($_REQUEST['UserName'] == "wang" && $_REQUEST['Password'] == "1234"){
      $_SESSION['UserName'] = "wang";
      $_SESSION['Password'] = "1234";
      header("Location: index.php");
   }
   else{
      header("Location: login.php");
	  $_SESSION['message'] = "Incorrect username/password";
          exit;
   }
?>

Open in new window

Hello qqchen,


Please change your else part code to following.. and see if it helps.

Thanks.
else{
      $message="Incorrect username/password";
      header("Location: login.php?message=".$message);
	  
   }

Open in new window

Avatar of qqchen
qqchen

ASKER

i tried both of your code, it looks still not work. T_T User generated image User generated image
you need to put session_start(); in login.php to use sessions
Hello

in login.php, you should have following,

$message = $_GET["message"];

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Welcome to EE, qqchen.  Here are some ideas to make your use of this site more enjoyable and productive for you...

At EE, the experts exchange answers and advice for points.  If you look at the questions awaiting answers in this zone, you will see a lot of 500 point questions.  Your question is competing for the experts' attention among those high-point questions.  So as a matter of simple economics you might be able to envision which questions will get the experts' attention first.   Just a thought.

We are experts, but not mind readers.  Inquiries that are broad, vague and hypothetical may not get answers that are as succinct and effective as inquiries that have actual URLs, complete code examples, and clearly expressed questions.  "It doesn't work" is not an error message.  Whenever possible, please provide the inputs and tell us what you want for the outputs.  Sometimes the right answer is, "Don't do that -- it doesn't work that way."

If you want us to be able to share working code, we need you to show us where you have put your test data.  If you have no test data, please create some.  We do not want you to post "live" passwords and such.  Instead, please set up a testbed and show us the links to that, instead of the live data.

We answer questions and provide teaching examples, but we cannot build your applications for you.  If you do not understand the basics of computer science and the programming languages involved in your applications, you might be better off to hire a developer.  Often a great deal of trial and error, plus a depth of knowledge and background information is necessary to get a piece of an application working.  The experts will try to help, but sometimes the only reasonable answer is, "Please read the fine manuals" or "Don't waste your time -- hire a professional developer."

All of us who have been at EE for a while have seen questions like, "How do I do 'X' in 'Y' language, and by the way, I do not know anything about 'Y' language."  For some reason we never see anyone ask, "I want to play a piano sonata, and by the way, I have never taken piano lessons."  It is hardly a sin if you do not know a particular programming language -- I do not know most of them -- but it is not reasonable to expect that you will learn a programming language by asking questions in an online forum, any more than you could learn to play the piano by asking questions in an online forum.  Instead your best question might be, "What are good learning resources to get a foundation in 'Y' language?"  We are glad to help with that.

This article teaches the principles behind what you are trying to do:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

All the best, ~Ray
Avatar of qqchen

ASKER

Thank u for helping.. it works now!!