Jump to content

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341523
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341534
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341544
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341576
Share on other sites

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.  :(

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-341667
Share on other sites

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!    :)

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342170
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342173
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342178
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-342286
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/67954-page-not-redirecting/#findComment-345634
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.