determined Posted September 4, 2007 Share Posted September 4, 2007 I am learning php as I go, and have gotten help with as far as I have gotten. But now we are stuck and not sure what to do now. My goal: A user will log in using the form and then be redirected to a page set up special for them. How it is set up now: The login form <? include("global.php"); include("header.php"); ?> <form method="post" action="login_action.php"> <p>Username: <input type="text" name="username" /></p> <p>Code: <input type="text" name="password" /></p> <p><input type="hidden" name="submitted" value="true" /> <input type="submit" value="Login" /></p> </form> <?php include("footer.php"); ?> From there it goes to login_action <?php include("global.php"); if($_POST['submitted'] != true) { // did not come from login form header("Location: login.php"); } if($_SESSION['logged'] == true) { // already logged in header("Location: affiliate.php"); } $sql = mysql_query("SELECT package FROM `people` WHERE username = '".mysql_real_escape_string($_POST['username'])."' AND password = '".mysql_real_escape_string($_POST['password'])."'"); if(mysql_num_rows($sql) == 1) // should only be one record { $row = mysql_fetch_assoc($sql); // log user in $_SESSION['logged'] = true; // set affiliate package in session $_SESSION['package'] = $row['package']; // redirect to affiliate page header("Location: affiliate.php"); } else { // username and password not found header("Location: login.php"); } ?> From the login_action it is supposed to go to either affiliate_a.php or affiliate_b.php depending on which one they are assigned in my mySQL database. The mySQL database is set up like this: Database name Affiliates Table name People Fields: name code package I even set up a little form to add Names, Codes and the packages to the database http://www.maxwho.com/testLogin.html My lil dilemma: Currently, when I go to http://www.maxwho.com/login.php and type in my username user1 and code use123 it goes to http://www.maxwho.com/login_action.php and just stops. It doesn't go to package a or package b pages. I have no idea where to go from here and my other help is stumped. I appreciate any bit of advice you can offer. Maybe even a great online resource for learning php. Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/ Share on other sites More sharing options...
micah1701 Posted September 4, 2007 Share Posted September 4, 2007 from first glance, it looks like you just need to change: if($_POST['submitted'] != true) to if($_POST['submitted'] != "true") because you are assigning a text value of "true" to the post array. by leaving out the quotes in your method, your script is looking to see if the value is litterally true or 1. but its a string, so just put the "true" in quotes and you should be good. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341523 Share on other sites More sharing options...
lemmin Posted September 4, 2007 Share Posted September 4, 2007 It should still direct somewhere, no matter what any of the conditions are. Try putting an echo statement somewhere in that code without a condition. My guess is that it won't show up. I think the problem actually lies in the page that it is being directed to. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341525 Share on other sites More sharing options...
darkfreaks Posted September 4, 2007 Share Posted September 4, 2007 Change: <?php header("Location: login.php"); ?> to: <?php header('Location: http://www.yoursite.com/login.php');?> Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341529 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 I tried the suggestion of putting the line if($_POST['submitted'] != 'true') instead of if($_POST['submitted'] != true) and that didn't work. The page that the login should be redirecting to is affiliate_a.php <?php include("global.php"); include("header.php"); ?> <p>prices for package a</p> <?php include("footer.php"); ?> The code for the global.php <?php session_start(); $conn = mysql_connect("localhost", "Affiliate1", "maximo") or die("no connection available."); mysql_select_db("Affiliates", $conn) or die("no database selected."); ?> The other person helping me before mentioned that maybe it has something to do with the session not being able to start on the server. Not really sure what that means. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341534 Share on other sites More sharing options...
lemmin Posted September 4, 2007 Share Posted September 4, 2007 That could make the redirection not work. Try using session_start() or die("didn't work"); for the session_start() and see if it prints that string somewhere. Otherwise, did you try putting an echo statement somewhere on your page, like I suggested? Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341536 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 Thank you for your help! I put the echo line in my login_action.php page and it shows up. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341540 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 I also put the session line in the global.php page. So it looks like this now: <?php session_start() or die("didn't work"); $conn = mysql_connect("localhost", "Affiliate1", "maximo") or die("no connection available."); mysql_select_db("Affiliates", $conn) or die("no database selected."); ?> And it doesn't do anything different. The only different thing happening is the echo text showing up on the login_action.php page. Instead of redirecting to one of the affiliates pages. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341544 Share on other sites More sharing options...
lemmin Posted September 4, 2007 Share Posted September 4, 2007 Are errors turned off on that server? It seems like a server specific problem, to me. Try making a php file with just this in it: <?php header("Location: http://www.google.com"); ?> Put it on the server, run it, and see if it goes to google. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341556 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 I made a page called google.php and put it on the server with that code in it. It went to google. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341560 Share on other sites More sharing options...
lemmin Posted September 4, 2007 Share Posted September 4, 2007 What if you replace the echo statement that I had you put in earlier with a header redirection to google (or anywhere?) Tell me if that works. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341563 Share on other sites More sharing options...
darkfreaks Posted September 4, 2007 Share Posted September 4, 2007 all you need is <?php header('Location: http://yoursite.com/login.php');?> Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341571 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 That was supposed to be on the login_action.php page right? I put it on there so now that page looks like this: <?php include("global.php"); if($_POST['submitted'] != true) { // did not come from login form header('Location: http://www.maxwho.com/login.php'); } if($_SESSION['logged'] == true) { // already logged in header("Location: affiliate.php"); } $sql = mysql_query("SELECT package FROM `people` WHERE username = '".mysql_real_escape_string($_POST['username'])."' AND password = '".mysql_real_escape_string($_POST['password'])."'"); if(mysql_num_rows($sql) == 1) // should only be one record { $row = mysql_fetch_assoc($sql); // log user in $_SESSION['logged'] = true; // set affiliate package in session $_SESSION['package'] = $row['package']; // redirect to affiliate page header("Location: affiliate.php"); } else { // username and password not found header("Location: login.php"); } header('Location: http://maxwho.com/login.php'); ?> But it still isn't going anywhere... Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341576 Share on other sites More sharing options...
lemmin Posted September 4, 2007 Share Posted September 4, 2007 Try commenting out the session_start() function in the global.php file and see if it changes anything. @darkfreaks, why would single quotes change anything? Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341615 Share on other sites More sharing options...
darkfreaks Posted September 4, 2007 Share Posted September 4, 2007 LOL it prolly wouldnt i have seen it mainly used with single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341616 Share on other sites More sharing options...
determined Posted September 4, 2007 Author Share Posted September 4, 2007 Ok, I changed the global.php code <?php /* session_start() or die("didn't work");*/ $conn = mysql_connect("localhost", "Affiliate1", "maximo") or die("no connection available."); mysql_select_db("Affiliates", $conn) or die("no database selected."); ?> Still doesn't work though. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341667 Share on other sites More sharing options...
lemmin Posted September 5, 2007 Share Posted September 5, 2007 I think your server has error reporting turned off and that you have some whitespaces before or after your php blocks. Look carefully and make sure there are no spaces or returns before or after the <?php or ?> tags, respectively. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342110 Share on other sites More sharing options...
determined Posted September 5, 2007 Author Share Posted September 5, 2007 Thanks! Ok, I cleaned up all spaces around the php on my login_action, global, and login pages. It still isn't working. What do you mean by "I think your server has error reporting turned off" ? Is this something I have to ask my host to fix? Thank you so much for all of your help! Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342170 Share on other sites More sharing options...
lemmin Posted September 5, 2007 Share Posted September 5, 2007 In that file that you tested that just had the header() function in it, put some whitespaces before and after the <?php bock and see if it gives you an error. <?php header("Location: http://www.google.com"); ?> That should either give an error or show a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342173 Share on other sites More sharing options...
determined Posted September 5, 2007 Author Share Posted September 5, 2007 Just a blank page...hmmmm Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342175 Share on other sites More sharing options...
lemmin Posted September 5, 2007 Share Posted September 5, 2007 That means that error reporting IS turned off. You don't necessarily need to turn it on; the error you will be getting is something like "headers already sent." Make sure there are no whitespaces in any file that is associated with the file that contains the header() function (like global.php.) There is also a sticky about this that you can check, http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342178 Share on other sites More sharing options...
determined Posted September 5, 2007 Author Share Posted September 5, 2007 Thank you for the reference! I went through all of my php pages and made sure there isn't any white spaces around the php blocks. On that link you sent me to, I don't understand. I am learning all of this as I go, so if you could help me understand this I would appreciate it. answer: put the processing in the header, and store the results in variables. perhaps a $result variable that is 1 if successful, 0 if failed. then $output that contains either a success message or customized error messages. the new code would look like: [html starting the page and layout] [php echoing the results] [form code if failed - exit(); if successful][/quote] This is what my pages look like currently: global.php [code]<?php //session_start() or die("didn't work"); $conn = mysql_connect("localhost", "Affiliate1", "maximo") or die("no connection available."); mysql_select_db("Affiliates", $conn) or die("no database selected.");?> [/code] login_action.php [code]<?php include("global.php"); if($_POST['submitted'] != true) {// did not come from login form header('Location:http://www.maxwho.com/login.php');} if($_SESSION['logged'] == true) { // already logged in header("Location:affiliate.php"); } $sql = mysql_query("SELECT package FROM `people` WHERE username = '".mysql_real_escape_string($_POST['username'])."' AND password = '".mysql_real_escape_string($_POST['password'])."'"); if(mysql_num_rows($sql) == 1) // should only be one record { $row = mysql_fetch_assoc($sql); // log user in $_SESSION['logged'] = true; // set affiliate package in session $_SESSION['package'] = $row['package']; // redirect to affiliate page header("Location: affiliate.php"); } else { // username and password not found header("Location: login.php"); }?> [/code] login.php [code]<?php include("global.php"); include("header.php");?> <form method="post" action="login_action.php"> <p>Username: <input type="text" name="username" /></p> <p>Code: <input type="text" name="password" /></p> <p><input type="hidden" name="submitted" value="true" /> <input type="submit" value="Login" /></p> </form> <?php include("footer.php");?>[/code] affiliate.php [code]<?php include("global.php"); include("header.php"); if($_SESSION['logged'] != true) {// not logged in header('Location: http://www.maxwho.com/login.php'); } switch($_SESSION['package']) { case 'a': include "affiliate_a.php"; break; case 'b': include "affiliate_b.php"; break; default: header("Location: login.php"); break; } include("footer.php");?> [/code] affiliate_a.php [code]<?php include("global.php");include("header.php");?> <p>prices for package a</p> <?php include("footer.php");?>[/code] header.php [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>Untitled Document</title> </head> <body> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342286 Share on other sites More sharing options...
determined Posted September 10, 2007 Author Share Posted September 10, 2007 I managed to get this code working. It works to register a username and password, adding them to my database. Now I have the other code logging in with that username and password. So this all works wonderfully. What I would like to do is redirect the page it logs onto depending on which password they use. Any ideas as to how I can do this? This is my index page <? // connect to database include("inc/connect.php"); // include auth and nav include("inc/auth.php"); // begin content include("inc/nav.php"); // close mysql connection mysql_close(); ?> The nav page <? // Login & Session example by sde // nav.php ?><table align="center"> <tr><td colspan="2"><img src=/inc/maximoLogoLogin.jpg> <br> <hr></td></tr> <tr> <td>.... connect page <? // Login & Session example by sde // connect.php // replace with your db info $hostname="mysql"; $mysql_login="mynewuser"; $mysql_password="mynewpass"; $database="newdatabase"; if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){ die("Can't connect to mysql."); }else{ if (!(mysql_select_db("$database",$db))) { die("Can't connect to db."); } } ?> auth page <? // start session session_start(); // convert username and password from _POST or _SESSION if($_POST){ $_SESSION['username']=$_POST["username"]; $_SESSION['password']=$_POST["password"]; } // query for a user/pass match $result=mysql_query("select * from users2 where username='" . $_SESSION['username'] . "' and password='" . $_SESSION['password'] . "'"); // retrieve number of rows resulted $num=mysql_num_rows($result); // print login form and exit if failed. if($num < 1){ echo "this is where my page info is"; exit; } ?> Thank you for taking a look and maybe helping me with suggestions!! Quote Link to comment https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-345634 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.