Jump to content

else if login page trouble


sjslovechild

Recommended Posts

Ok here is what I'm trying to do.

 

There is a link in a email that is sent out to the people who use this app.

When they click on the email link they are brought to this site which is password protected.

So they have to enter their username and password. What I want to below script to do is to log them in while checking to see if the $id variable is set. If it is, then the script is to take them to the page in the email link. If not take them to the submit job page.

 

<?php
$id=$_POST["id"];
$cmd = $_POST['cmd'];
$connection = mysql_connect("host", "user", "pass");
mysql_select_db("database", $connection) or die(mysql_error());


switch($cmd) {
case "login":
	$u = $_POST['username'];
	$p = $_POST['password'];
	$query = "SELECT * FROM login WHERE username='$u' AND password='$p'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);

	if (isset($id))($row){

		session_start();
		$_SESSION['user_id'] = $row[0];
		$_SESSION['residentname'] = $row[1];
		$_SESSION['unit_num'] = $row[2];
		setcookie("TestCookie", time()+3600);  /* expire in 1 hour */
		$resite = "submitjob.php?do=viewone&id=$id";
		header("Location:$resite");
		exit();

	} else if ($row){

		session_start();
		$_SESSION['user_id'] = $row[0];
		$_SESSION['residentname'] = $row[1];
		$_SESSION['unit_num'] = $row[2];
		setcookie("TestCookie", time()+3600);  /* expire in 1 hour */
		$resite = "submitjob.php";
		header("Location:$resite");
		exit();


	} else {
		echo "Sorry the app didn't find a match.";
	}
break;



}
?>

Link to comment
https://forums.phpfreaks.com/topic/225023-else-if-login-page-trouble/
Share on other sites

You did not clearly state a problem, so this "solution" might not solve it;

 

This statement:

if (isset($id))($row){

does not seem to be syntactically correct to me. Did you, perhaps, mean:

if ((isset($id)) AND ($row)) {

 

However, isset($id) will always be true there, because you set it at the very first line. Perhaps a better test would be:

if ((!empty($id)) AND ($row)) {

 

Plus you have some duplicated code there. I would probably re-write the entire IF statement as:

	if ($row) {
	session_start();
	$_SESSION['user_id'] = $row[0];
	$_SESSION['residentname'] = $row[1];
	$_SESSION['unit_num'] = $row[2];
	setcookie("TestCookie", time()+3600);  /* expire in 1 hour */
	$resite = "submitjob.php";
	if (!empty($id)) $resite .= "?do=viewone&id=$id";
	header("Location: $resite");
	exit();
} else {
	echo "Sorry the app didn't find a match.";
} 

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.