hi
all my login is working,but when my url of the third page is typed it is displaying .i want to autheticate using session but it is not working


z-login(1st page)

<form name="form1" method="get" action="z_login1.php">
User Name<input type="text" name="myusername" style="width: 10em;">
Password</strong></td><td><input type="password" name="mypassword" style="width:10em;">
<input type="submit" name="submit" value="Login">
</form>

z_login1(2nd page)

<body>
<?php
$hostname = "localhost";
$username = "l";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$myusername=$_REQUEST['myusername'];
$mypassword=$_REQUEST['mypassword'];
$sql="SELECT username,password FROM login WHERE username='$myusername' and password='$mypassword' and type='0'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
for ($i = 0; $i < mysql_num_rows($result); ++$i)
{
$line = mysql_fetch_row($result);
$_SESSION["un"] = $line[0];
}
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
print "<script>";
print " window.location='http://www.web.com/z_login2.php'";
print "</script>"; 
}
else
{
 print "<script>";
print " window.location='http://www.web.com/z_login.html'";
print "</script>";  
}
>
</body>

z_login2(3rdpage)

<? session_start();
	if (!isset($_SESSION['un'])){
exit();		header("Location: z_login.html");
}
else
{
?>

<html>
<head></head>
<body>
<?
 print "Thank visiting";
}
?>
</table>
</body>
</html>

please do tell the error in the code

Recommended Answers

All 11 Replies

i dont see session_start in page2 ?

No its there

<?php
   session_start();
  ?>
<html>
<head></head>
<body>
<?php
$hostname = "";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$myusername=$_REQUEST['myusername'];
$mypassword=$_REQUEST['mypassword'];
$sql="SELECT username,password FROM login WHERE username='$myusername' and password='$mypassword' and type='0'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
for ($i = 0; $i < mysql_num_rows($result); ++$i)
{
$line = mysql_fetch_row($result);
$_SESSION["un"] = $line[0];
}
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
print "<script>";
print " window.location='http://www/web.com/z_login2.php'";
print "</script>"; 
}
else
{
 print "<script>";
print " window.location='http://www.web.com/z_login.html'";
print "</script>";  
}
?>
</body>
</html>

hmm.. What exactly is the error ?

when the uRL of the third page is typed i am able to see the third page.i want all tha user to enter from login oage...if they enter the url of the third page it should redirect them to login page.....i am not able to get this....some error third page

take off the exit in 3rd page. put it after header.

it is working but my login is not working.even when i type the correct username and password it is redirecting to the login page itself..........

Then something is wrong with the 2nd page. Try this instead.

<?php
   session_start();
  ?>
<html>
<head></head>
<body>
<?php
$hostname = "";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$myusername=$_REQUEST['myusername'];
$mypassword=$_REQUEST['mypassword'];
$sql="SELECT username,password FROM login WHERE username='$myusername' and password='$mypassword' and type='0'";
$result=mysql_query($sql);
// If result matched $myusername and $mypassword, table row must be 1 row
if(mysql_num_rows($result) > 0) { // that means, a record exists for that username and password
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['un']=$myusername;
print "<script>";
print " window.location='http://www/web.com/z_login2.php'";
print "</script>"; 
}
else
{
 print "<script>";
print " window.location='http://www.web.com/z_login.html'";
print "</script>";  
}
?>
</body>
</html>

thanks tat is working.

:) You are welcome!

hi i have button called apply ,if he is a logged user then it should redirect ti Z_login2.php else to the login page

i tried some thing like this .....but it did not work

function login()
{
if($_SESSION['un']="")
{
 print "<script>";
print " window.location='http://www.careerglitters.com/web_login.html'";
print "</script>"; 

}
else
{
print "<script>";
print " window.location='http://www.careerglitters.com/web_login2.php'";
print "</script>"; 
}
}
 <input type='submit' name='submit' value='Apply Online' onSubmit=login(); style='height :2em; width :7em; font-weight:bold; font-size:0.7em' >

Have a form. When he clicks on the button, do the validation.

if(isset($_POST['submit'])){
//check if the session is set
if set, redirect to the correct page 
 else , redirect to login page
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.