wmguk Posted March 11, 2010 Share Posted March 11, 2010 Hey, I'm testing some code I've had written and I cannot get it working at all...! I have a login section, enter your username and password and its either 1 or 2 returned... public function login() { $output = $this->user->login($_POST['email'],$_POST['password']); if (isset($_POST['purchase'])) { $output['purchase'] = 1; } echo json_encode($output); exit; } public function login($user,$pass) { $sql = "select * from users where email = ? and password = ?"; $params = array($user,$pass); $i = $this->db->query($sql,$params); if ($i->num_rows) { $row = $i->result(); $_SESSION['user']['id'] = $row[0]->id; $_SESSION['user']['username'] = $row[0]->first_name." ".$row[0]->last_name; $_SESSION['loggedIn'] = 1; setCookie('userId', $row[0]->id, time()+60*60*24*2,'/',COOKIE_DOMAIN); setCookie('userName', $_SESSION['user']['username'], time()+60*60*24*2,'/',COOKIE_DOMAIN); $output = array('userName' => $_SESSION['user']['username'], 'status' => 1); } else { $output = array('status' => 0); } return $output; } at the moment you enter your username and password and I just get a page showing the $output.... Any ideas how to edit this so it directs to an actual login page - once you login it needs to go to the /index.php and show logged in... Link to comment https://forums.phpfreaks.com/topic/194888-grabbing-results-from-a-function/ Share on other sites More sharing options...
cags Posted March 11, 2010 Share Posted March 11, 2010 I don't see how you want it to direct to a login page, surely this is a function that would be called FROM a login page. Personally I'd probably return TRUE if the login is successful (ie within the if block) and return FALSE otherwise (ie anything that doesn't enter the if block). Then wherever you call the function you will use something like... if( $some_object->login( $user, $pass ) ) { header('Location: /index.php'); } else { // show login page with error } This of course makes many assumptions as to the layout of your 'application'. Link to comment https://forums.phpfreaks.com/topic/194888-grabbing-results-from-a-function/#findComment-1024726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.