4 Replies - 247 Views - Last Post: 16 October 2017 - 09:14 AM Rate Topic: -----

#1 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Passing variables from another script and page redirect

Posted 14 October 2017 - 12:10 PM

Hello DICers,
Im trying to wire up two pages: userlogin.php and lotto.php. After entering a validated user credential i want the user to click the hyper link and be sent to lotto.php with the auth users first and last name. The userlogin page works excepts when i click the hyper link "buy lotto tickets" to go back to the userlogin page.
On the lotto.php i have added include function and added userlogin.php and declared global variables f and lname. now when i try to run the lotto.php page from the browser it goes to the userlogin.php

im not sure why this is. please see code below, Thanks in advance for the help

userlogn.php
<?php

//check for required fields from the form
if ((!filter_input(INPUT_POST, 'username'))
        || (!filter_input(INPUT_POST, 'password'))) {
//if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) {
	header("Location: userlogin.html");
	exit;
}

//connect to server and select database
$mysqli = mysqli_connect("localhost", "cs213user", "letmein", "testDB");

//For more info about mysqli functions, go to the site below:
//http://www.w3schools.com/php/php_ref_mysqli.asp

/* create and issue the query
$sql = "SELECT f_name, l_name FROM auth_users WHERE username = '".$_POST["username"].
        "' AND password = PASSWORD('".$_POST["password"]."')";
*/

//create and issue the query
$targetname = filter_input(INPUT_POST, 'username');
$targetpasswd = filter_input(INPUT_POST, 'password');
$sql = "SELECT f_name, l_name FROM auth_users WHERE username = '".$targetname.
        "' AND password = PASSWORD('".$targetpasswd."')";

$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

//get the number of rows in the result set; should be 1 if a match
if (mysqli_num_rows($result) == 1) {

	//if authorized, get the values of f_name l_name
	while ($info = mysqli_fetch_array($result)) {
		$f_name = stripslashes($info['f_name']);
		$l_name = stripslashes($info['l_name']);
	}

	//set authorization cookie
	setcookie("auth", "1", time()+60*30, "/", "", 0);

	//create display string
	$display_block = "
        <body style='background-color:bisque'>
	<p>".$f_name." ".$l_name." is authorized!</p>
	<p>Authorized Users' Menu:</p>
	<ul>
	<li><a href=\"lotto.php\">Buy Lotto Tickets</a></li>
	</ul>
        </body>";
} else {
	//redirect back to login form if not authorized
	header("Location: userlogin.html");
	exit;
}
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<?php echo "$display_block"; ?>
</body>
</html>





lotto.php
<body style='background-color:bisque'>
<form method="POST" action="">
    <p><strong>Welcome ".$f_name." ".$l_name."</strong><br><br>
      Select either or both lotteries to play.<br><br>
      <label for="Lotto6">Lotto 6/49 numbers</label>
      <input type="checkbox" id="Lotto6" name="six" value="Lotto 6/49">
      <label for="Lotto7"> Lotto MAX (7/49)</label>
      <input type="checkbox" id="Lotto7" name="seven" value="Lotto Max"><br />

  <p><input type="submit" value="submit"/></p>
 
</form>
</body>

<?php
include('userlogin.php');
global $f_name;
global $l_name;

function lotteryNumbers($min_number, $max_number, $amount_of_numbers)
{
    $range = range($min_number, $max_number);
    shuffle($range);
    $numbers_array = array_slice($range, 0, $amount_of_numbers);
    natsort($numbers_array);
    return array_values($numbers_array);
}

if (isset($_POST['six'])) {
        echo '<img src="/lotto6_49.png" alt="Lotto6/49" height="80" width="120"/></br>';
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 6);
            echo implode(",", $number_set) . "<br />";
        }
}

if (isset($_POST['seven'])) {
        echo '<img src="/lottoMAX.png" alt="LottoMAX" height="80" width="120"/></br>';
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 7);
            echo implode(",", $number_set) . "<br />";
        }
}

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Passing variables from another script and page redirect

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,896
  • Joined: 12-June 08

Re: Passing variables from another script and page redirect

Posted 14 October 2017 - 12:26 PM

Have you considered session variables?
https://www.w3school...hp_sessions.asp

https://secure.php.n...ssion-start.php
Was This Post Helpful? 0
  • +
  • -

#3 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Re: Passing variables from another script and page redirect

Posted 14 October 2017 - 01:37 PM

i made the changes but cant see the error in the html output of the session details
<body style='background-color:bisque'>
<form method="POST" action="">
    <p> <strong><?php echo "Welcome".$_SESSION["f_name"].""
                  .$_SESSION["l_name"].;?>
        </strong><br><br>
      Select either or both lotteries to play.<br><br>
      <label for="Lotto6">Lotto 6/49 numbers</label>
      <input type="checkbox" id="Lotto6" name="six" value="Lotto 6/49">
      <label for="Lotto7"> Lotto MAX (7/49)</label>
      <input type="checkbox" id="Lotto7" name="seven" value="Lotto Max"><br />

  <p><input type="submit" value="submit"/></p>
 
</form>
</body>

<?php
session_start(); //get session variables from userlogin.php
function lotteryNumbers($min_number, $max_number, $amount_of_numbers)
{
    $range = range($min_number, $max_number);
    shuffle($range);
    $numbers_array = array_slice($range, 0, $amount_of_numbers);
    natsort($numbers_array);
    return array_values($numbers_array);
}

if (isset($_POST['six'])) {
        echo '<img src="/lotto6_49.png" alt="Lotto6/49" height="80" width="120"/></br>';
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 6);
            echo implode(",", $number_set) . "<br />";
        }
}

if (isset($_POST['seven'])) {
        echo '<img src="/lottoMAX.png" alt="LottoMAX" height="80" width="120"/></br>';
  $number_set = array();
        for ($i = 1; $i <= 6; $i++) {
            $number_set = lotteryNumbers(1, 49, 7);
            echo implode(",", $number_set) . "<br />";
        }
}

?>


Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: Passing variables from another script and page redirect

Posted 14 October 2017 - 04:25 PM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Passing variables from another script and page redirect

Posted 16 October 2017 - 09:14 AM

You have session_start too far down the page, you need to use session_start before sending any output to the browser. You should consider re-organizing all of that code in general, if a PHP page has any output (and especially if it may have output, or may not) then you should have all of your PHP code at the top of the file, and only do the output later after the PHP code is finished doing whatever it needs to do. You have it backwards, you're outputting HTML first and then running the PHP. That should be switched.

It's also a little weird that you're including one of those files from the other one. That's going to end up with 2 HTML bodies, which isn't going to work. If PHP is producing HTML output then it should produce one valid HTML document in all cases.

Also, using global where you did doesn't do anything, in that scope those variables are already global. You would use global inside a function if you want to have that function use the global version of a variable instead of one local to only that function, that's the only place you use global. Global does not do anything with making the same variable accessible across multiple browser requests, that's what sessions are for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1