Jump to content

random value change on next page


I-AM-OBODO
Go to solution Solved by I-AM-OBODO,

Recommended Posts

Hi all.

I generated a random number and assigned it a variable to be used through out the session but on getting to the next page, the value changes. It is regenerating another number which isnt the intention. I have tried severally but no way!

i really cannot figure out why the value changes in my second page.

 
$loan =  mt_rand(1000, 9999);
$name = "John";
 
if(isset($_POST['continue'])){
    
$_SESSION['num'] = $number;
 
$sql = ("INSERT INTO table (name, token_number) VALUES(:name, :token_number)
$stmt=$pdo->prepare($sql);
$stmt->execute(array(
':name'=>$name;
':token_number'=>$_SESSION['num']
));
 
if($stmt->rowCount()==1){
 
header("location: nextpage.php");
 
}else{
 
echo "Something went wrong";
 
}
 
}
Link to comment
Share on other sites

Sounds to me you most likely need to wrap the code that generates the number in a condition, so it only generates the number when the session variable does not exist. Eg

if(!isset($_SESSION['num']))
{
    // generate new random number
}

// use random number
Link to comment
Share on other sites

 

Sounds to me you most likely need to wrap the code that generates the number in a condition, so it only generates the number when the session variable does not exist. Eg

if(!isset($_SESSION['num']))
{
    // generate new random number
}

// use random number

Tried that but still didnt work. still regenerates number

Link to comment
Share on other sites

Have you made sure you are calling session_start() before the use $_SESSION's

 

yes. i have other session value and all is working well except this one. funny enough there's no code generating function on the next page. but why does it regenerate?

Link to comment
Share on other sites

The code you posted, is that copy and pasted right from your work or did you modify it for us? It's broken by the looks of it.

 

Maybe it's throwing an error but redirecting to the other page before you see the message. Comment out the header and see what happens.

Link to comment
Share on other sites

There seems to be a little problem with my first post but the problem still lingers.

I generate the code on the first page so that it can pass on the value generated to the second page.

here's a modification of my code

//page one
session_start();

//generate the number
// i even tried $_SESSION['number'] =  mt_rand(1000, 9999); but to no avail
$number = mt_rand(1000, 9999);

$name = "John Doe";

echo $name;
echo "<br>";
echo "Your tally nuber is: ". $number ;

if(isset($_POST['submit'])){

//database insert stuff
$_SESSION['number'] = $number;
header("location: nextpage.php");

}else{

echo "could not complete action";

}
//html form cos
<form method="post" action="">
<input name="submit" type="submit" value="NEXT" >
</form>


//page two

session_start();

echo $_SESSION['number']; //but it regenerates another number
 
Link to comment
Share on other sites

The code you posted, is that copy and pasted right from your work or did you modify it for us? It's broken by the looks of it.

 

Maybe it's throwing an error but redirecting to the other page before you see the message. Comment out the header and see what happens.

 

i doubt there's any error

Link to comment
Share on other sites

Try this for page one

<?php
//page one
session_start();
if(isset($_POST['submit'])){
$_SESSION['number'] = mt_rand(1000, 9999);

//database insert stuff
header("location: nextpage.php?session=".$_SESSION['number']."&name=".trim($_POST['name']));
exit;
}
?>

<form method="post" action="">
<input name="name" type="text" value="" >
<input name="submit" type="submit" value="NEXT" >
</form>
Link to comment
Share on other sites

Changed it around a little more.

 

page one

<?php
session_start();
if(isset($_POST['submit']) && trim($_POST['name']) != ""){
if(!isset($_SESSION['number'])){
$_SESSION['number'] = session_id();
}
if(!isset($_SESSION['name'])){
$_SESSION['name'] = trim($_POST['name']);
}
//database insert stuff
header("location: nextpage.php");
exit;
}else{
echo "Enter your name";	
}
?>

<form method="post" action="">
<input name="name" type="text" value="" >
<input name="submit" type="submit" value="NEXT" >
</form>

page two

<?php
session_start();

echo "session: ".$_SESSION['number']."<br />";
echo "name: ".$_SESSION['name'];
?>
Edited by QuickOldCar
Link to comment
Share on other sites

header("location: nextpage.php");
[.....truncated]
<form method="post" action="">

Quick question: how come you did it that way -- with the header: location next page, instead of the classic way of "<form action="nextpage.php">

 

I was always taught to use <form action="thepage_I_want.php">.

 

Is this a newer, better way of doing things?

Edited by Torrie
Link to comment
Share on other sites

  • Solution

Thanks all.

 

Finally i got it to work. I noticed that the point where the regenerating starts is not the next page but on the first page (reason i don't understand though)

I had to create a hidden field and assign the random number to it and then i did an insert with the post value and after that reassigned the value to a session.

Viola! it did the magic!

 

PS: The code shown is not the complete code but the modified code/logic that was giving me trouble and that worked the trouble.

 

Here's my code

 

 

 
$number = mt_rand(1000, 9999);

$name = "John Doe";

echo $name;
echo "<br>";
echo "Your tally nuber is: ". $number ;

if(isset($_POST['submit'])){

//database insert stuff
$_SESSION['numb'] = $_POST['numb'];
header("location: nextpage.php");

}else{

echo "could not complete action";

}
//html form
<form method="post" action="">
<input name="numb" type="hidden" value="<?php echo $number; ?>">
<input name="submit" type="submit" value="NEXT" >
</form>


//page two

session_start();
 
$number = $_SESSION['numb'];

echo $number;
 
Edited by Mr-Chidi
Link to comment
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.