File name as variable+.txt [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
session_start();
$user = $_SESSION['username'];
if( isset($_POST['subm_btn']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
}
function incrementClickCount()
{ $count = getClickCount() + 1;
file_put_contents($user.".txt", $count);
}
User register on my site, then he click on button (name="subm_btn"). I want count clicks and add number of clicks in file with name "username.txt"

I guess you are looking for something like this:
$file=$user.'.txt';
incrementClickCount($file);
function incrementClickCount($file){
$count = getClickCount($file) + 1;
file_put_contents($file, $count);
}
function getClickCount($file) {
return (int)file_get_contents($file);
}
If you want the variable to be available inside a function you either make it global or pass it as an argument (which is better).

You define $user, and then access $user1. That would be my guess as to why it doesn't work. Also, using $file might be a better idea anyway.

Related

How to state 2 or more different consequences if an if statement condition is true? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to have 2 things happen if an if statement condition is met. If isset is true the variable $value will equal 1. The problem is that I also want the value of a session variable $_SESSION['counter'] to increment by 1, but I am unsure of the syntax.
<?php
session_start();
$_SESSION ['counter']=0;
function checkbox_boolean ($name){
if (isset($_POST[$name])) $slot1 = '1'; $_SESSION['counter']++; else $slot1='0';
}
This will work and its a better solution:
<?php
session_start();
$_SESSION['counter']=0;
function checkbox_boolean ($name){
$slot1=0;
if(isset($_POST[$name])){
$slot1 = 1;
$_SESSION['counter']++;
}
}
?>

Call a php function passing argument on a link in the same page and display the content in div [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to call a function by passing argument on a HTML link in the same page
My code:
<?php
#array loop
foreach($new_array as $value){
#pass the array to the function
$input = inputFiles($value);
# call the function on the click of link
echo 'Input Link';
# output of the function in the div
echo '<div id="inputLinkOutput" ></div>';
}
function inputFiles($value){
# get the value and do the rest work
}
?>
Please help.
So you pass $_GET variable via url like that:
<!-- lcoalhost/myPage.php -->
<a href='myPage.php?var1=123&var2=abc+bcd'>Link here</a>
And in PHP:
$var1 = $_GET['var1']; // => (string) '123'
$var2 = $_GET['var2']; // => (string) 'abc bcd'
You need to use something like Ajax to achieve this. Here how you do this.
Create a ajax function which calls a method in a php file. Along with the call pass a parameter with a value. Because this will help in the server side to execute which function.
Then inside the PHP file create set of functions which you think will execute. (PHP function has been listed below)
Introduce a switch statement to navigate to specific function.
$param = $_GET['val'];
switch($param){
case '1' : methodA(); break;
case '2' : methodB(); break;
//and so on
}
methodA(){
//do some logic
}
methodB(){
//do some logic
}
Hope this helps.
Cheers!

how to post SQL variable through session and get it in next page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to post $dataHeadArr value through session and get it in next page.
<?php
if(isset($_POST["search"]))
{ $storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT *FROM `opening_stk`");?>
On for the page you have posted put
<?php
if(isset($_POST["search"])){
session_start();
$storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT * FROM `opening_stk`");
while($row=mysql_fetch_assoc($dataHeadArr)){
$allRows[]=$row;
}
$_SESSION['dataHeadArr']=$allRows;
}
?>
For the page that you want to retreive the variable put
<?php
session_start();
$dataHeadArr=$_SESSION['dataHeadArr'];
//This mysql result been parsed already, so you should be
//able to access values when you loop through it from here
for($i=0;$i<count($dataHeadArr); $i++){
echo "Printing row ".$i."</br>";
foreach($dataHeadArr[$i] as $key=>$item){
//Prints out all value for the row
echo $item[$key]."</br>";
}
echo "</br>";
}
?>
You may just set it in the POST or SESSION array and then retrieve it on next page. For ex-
$_POST['dataHeadArr']=$dataHeadArr;
To store...
$_SESSION['dataHeadArr'] = 'Whatever';
Then to retrieve simply..
if(isset($_SESSION['dataHeadArr'])){
echo $_SESSION['dataHeadArr'];
}
Make sure to call session_start(); at the top of every page.

Creating custom ahref links in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you create a custom links in ahref using php as shown below
link from : http://www.top10bestwebsitehosting.com/visit.php?site=iPage
goes to : http://www.ipage.com
Perhaps something like this:
$sites = array(
'iPage' => 'http://www.ipage.com/',
'Google' => 'http://www.google.com/',
);
$key = $_GET['site'];
if(isset($sites[$key])) {
header('Location: ' . $sites[$key]);
exit;
}
echo 'Sorry, no such site.';
There is redirect, if you open in firefox developer tools and after open a link, and pressing escape key(else you will be redirected) you can see the next url:
http://naturaltracking.com/redirect.php?url=http%3A%2F%2Fwww.ipage.com%2Fjoin%2Findex.bml%3FAffID%3D638751%26amp%3BLinkName%3D&uid=6344b6a239bb&sid=374eaa4bb6d2&hukc=1&nicid=00004ZQNza&nivkey=FiiRIhiDHa
It was most likely done in this way:
have an internal list of what $_GET['site'] can be
read $_GET['site']
redirect to the corresponding website
This is an example:
<?php
// visit.php
$pages = array('iPage'=>'www.ipage.com','google','www.google.com');
if (in_array($pages,$_GET['site']))
{
// do some internal logging or whatever
header("Location: ".$pages[$_GET['site']]);
}
?>
The way top10bestwebsitehosting does is, it keeps the urls in the database. So then it does something similar to the below:
$name = mysql_real_escape_string($_GET['site']); //check for escapes etc.
$query = mysql_query('select * from `websites` where `name` = {$name} limit 1');
$row = mysql_fetch_row($query);
header('Location: '.$row['url']);

Variable in page title? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a page that contains a DB query, If there is a county in the URL then I run another query.
<head>
<title>Query results in $county</title>
</head>
...
if (isset($county)) {
// If county is set
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND County = :county");
} else {
// Run another query
}
I want to add the county in the page title if the county is set. Is this possible?
if (isset($county)) {
echo "<title>Query results in $county</title>\n";
} else {
echo "<title>Some other title</title>\n";
}
Absolutely. Just make sure your php that sets $county comes before the start of your html, then change your title tag to this:
<title>Query results in <?php echo $county;?></title>
Edit: Although j08691's answer is more comprehensive.

Categories