Flood Data in PHP form - php

I want to flood some random data in a PHP form. Can I do it?
I want to actually test my website and database ofcourse. All I want to know that is it capable of handling if multiple registration is done at same time.

cURL is a very good tool to fill up a (POST) form a submit it. You may find a cURL library implementation in PHP, so you can use a "familiar" language to get random data, but you can also use the command line version.

First, create pages that echo data from source.
data1.php
data2.php
data3.php
data4.php
Second create a php page that will do the following:
Create variables from data sources.
Pass variables into array.
Randomly select a specific variable.
Echo results into page.
// variables from data
$var1 = file_get_contents('data1.php');
$var2 = file_get_contents('data2.php');
$var3 = file_get_contents('data3.php');
$var4 = file_get_contents('data4.php');
// pass variables into array
$data = array($var1, $var2, $var3, $var4,);
$dkey = array_rand($data); // random selection
echo 'Data: '.$data[$dkey]; // echo selection
Your finished code will look like this:
<?php
$var1 = file_get_contents('data1.php');
$var2 = file_get_contents('data2.php');
$var3 = file_get_contents('data3.php');
$var4 = file_get_contents('data4.php');
$arr = array($var1, $var2, $var3, $var4,);
$key = array_rand($arr);
echo 'Data: '.$arr[$key];
?>

Related

PHP echo session value to a new session?

Is it possible to achieve something like this?
$_SESSION["new"] = "This are the values of $_SESSION['A'] & $_SESSION['B']";
I want to echo the "new" session value on a different page for my user to check.
I tried various things such as the code below but it doesn't work.
$_SESSION["new"] = <?php echo $_SESSION['A']?>;
Yes, you can assign multiple session variables by using the concatenation operator.
$_SESSION["new"] = "This are the values of ".$_SESSION['A']."&".$_SESSION['B'];
On the page, you want to display, simply print the session variable.
echo $_SESSION["new"];
Please make sure,on both pages, you are using session_start() function
$_SESSION["new"] = 'foo bar';
You can simply assign any value to your session variables. you can read more here
if you are sure of the type of value in $_SESSION["A"] and $_SESSION["B"], and assuming in your case they are both string, you can do the following:
$_SESSION["new"] = sprintf('This are the values of %s & %s', $_SESSION['A'], $_SESSION['B']);

How to echo variables randomized

I'm learning php on a very basic level and need som assistance.
I want to echo variables from one file to another. I'll try to reproduce:
folder/file1.php
file1.php contains 3 different php scripts:
php $var1 = "Hello"
php $var2 = "Goodbye"
php $var3 = "Nice"
And i want to echo one of the three variables randomly (scrambled) on various pages.
I use this now:
php include 'folder/file1.php'; echo get_($var1);
But it only displays the $var1 text.
How about:
<?php
$var1 = "Hello";
$var2 = "Goodbye";
$var3 = "Nice";
$i = rand(1, 3);
echo ${"var" . $i}
?>
At run time ${"var" . $i} is calculated to be $var1, $var2 or $var3.
Using php rand and dynamically created variable will let you do it. You can just continue to include your other files above this script and ensure the variables are named in a static way as shown above and it will work.
Example: Here (click edit then ideoneit)
put those vars in an array, and randomly select an element from the array:
$myItems = array("hello","goodbye","lovely");
echo $myItems[rand(0,2)];
http://us2.php.net/rand

Unable to create Dynamic Variables

When I check my cookies I do see that PHPSESSID is being made. But when I try to use them I get an error message saying that the variable is undefined.
This is how I set my Sessions:
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
if (isset($_POST[$post])) {
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
}
}
I also tried it manually like:
$_SESSION['auto_year'] = '2012';
But it still doesn't work. I did call session_start() on top of the pages of both but it just keeps giving me that error.
using ${} you can create dynamic variables. In your case just change to:
${$post} = $_SESSION[$post];
Another option is to create the variables using extract() which enables you to set all variables in one go;
extract($_SESSION);
Will create a variable for each key in the session, e.g. $auto_year, $auto_brand, etc.
http://php.net/manual/en/function.extract.php
note
I'm wondering why you want to have all data in separate variables, whereas a single associative array may be just as easy to handle?
It's likely something wrong with the $_POST data coming from your form. As I don't have form info, I tried this by setting $_POST.
session_start();
$_POST['auto_year'] = 1977;
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
echo $post."<br>";
if (isset($_POST[$post])) {
echo "Creating variable for $post<br>";
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
echo '$auto_year: '.$auto_year . "<br>";
}
}
echo "<br><br>".var_dump($_SESSION);

Changes to $_SESSION without page reload

I have two $_SESSION variables declared as array in index.php, say:
$_SESSION['a'] = array(1,2,3);
$_SESSION['b'] = array(4,5,6);
I make a AJAX call from index.php to result.php, where values of session variables are changed
to, say:
$_SESSION['a'] = array(2,3,1,4);
$_SESSION['b'] = array(5,6);
What I do is, I remove the first element from both the arrays and append it back to first array, $_SESSION['a'].
On successful AJAX call, I want to print the first elements of both the variables in index.php. Is it possible?
Yes, on a successful ajax call, you can do that.
This is pretty straight forward.
In your results.php you simply returned the new data with a bit of JSON.
result.php:
//set the session vars to whatever here....
//now, return them. In the example, I shall assign your session vars to temp vars.
$sessA=$_SESSION['a'];
$sessb=$_SESSION['b'];
echo json_encode(array('a'=>$sessA,'b'=>$sessb));
And now in your AJAX (assuming you're using jQuery):
$.getJSON('results.php', function(data){
alert(data.a);
alert(data.b);
});
And there you have it.
EDIT:
In your index.php, you say you set the session vars to the original value. But in your comments, you say you want to use the new vars in index.php. This won't be possible if you are setting them to the original value in index.php on every request. What you should do is check if they're already set first, and then do what needs to be done. Like this:
instead of:
$_SESSION['a'] = array(1,2,3);
$_SESSION['b'] = array(4,5,6);
Do this:
if ( isset($_SESSION['a']) === false && isset($_SESSION['b']) === false )
{
# - The vars are not already set...so it's okay to set them to its original value.
$_SESSION['a'] = array(1,2,3);
$_SESSION['b'] = array(4,5,6);
}

How to store all in a variable instead of echoing everything separately?

please see: http://pastebin.com/5za3uCi1
I'm quite new to php and I'm editing the ventrilo status script. What I'd like it to do is that it stores everything in one big variable for easy parsing instead of using separate echo's. Can someone tell me how I can accomplish this?
Thanks,
Dennis
You can use the output buffer and get the contents of it:
ob_start();
echo 'foobar';
$contents = ob_get_contents(); // now contains 'foobar'
ob_end_clean();
declare a variable at the beginning, say $data or whatever. then, replace the echo calls:
echo "hello";
with this:
$data .= "hello";
then return the $data variable at the end of the function.
Instead of the echo, you can use a simple affectation :
$request = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
But you'll soon have issues to manage multiple variables.
You could create an object to handle and store your information, but If you need something easy to set up and simple to operable, I'd go for arrays :
$ventriloStatus = array();
$ventriloStatus['requestObj'] = $stat->Request();
$ventriloStatus['requestMsg'] = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
Add your data using keys.
Then retrieve the value easily :
echo $ventriloStatus['requestMsg'];
You can even parse your data using a simple loop
foreach($ventriloStatus as $key => $value){
echo $key.' : '.$value.'<br />';

Categories