PHP - Make Scalar Variable Accessible Across Pages?
When login in (in login.php), the script is assigning user information to scalar variables like so:
$user_name = $assoc['user_name']; $user_id = $assoc['user_id']; Now I'd like to make use of those variables on other pages like this for example: echo "Posted by " . $user_name; I want that to be visible for everybody, even if the user logs out. Any ideas? Similar TutorialsOkay so my news script is set to view only 10 pieces of news. But I want it so that it starts a new page once I have more than 10 pieces of news. Code: [Select] <?php require("functions.php"); include("dbconnect.php"); session_start(); head1(); body1(); new_temp(); sotw(); navbar(); $start = 0; $display = 10; $query = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $display"; $result = mysql_query( $query ); if ($result) { while( $row = @mysql_fetch_array( $result, MYSQL_ASSOC ) ) { news_box( $row['news'], $row['title'], $row['user'], $row['date'], $row['id'] ); } mysql_free_result($result); } else { news_box( 'Could not retrieve news entries!', 'Error', 'Error', 'Error'); } footer(); mysql_close($link); ?> I tried a few things but they failed....miserably. Hi All I've a question regarding php and mysql database interrogation. Is there a way of applying a scalar database call from php, rather than having to return values in an array? For example if I know there is only 1 value in a DB table which matches a select statement, do I have to return it with the following or is there another function which will save me using arrays? Code: [Select] $sql="SELECT name FROM users WHERE id = 10"; $sql=mysql_query($sql); $row=mysql_fetch_array($sql); This would then give me $row['name'] to work with. Is there a way of doing this? In this array, I have 3 broken tools at $10.00 each: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 [broken] => 3 [broken_price] => 5.00 ) ) foreach($tools as $i => $val) { echo 'We have '.$val["broken"].' broken tools at '.$val["broken_price"].' each"; } Sometimes I have no broken tools, then the array will look like this: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 ) ) If I were to run the loop now, it would give an "undefined index" error. However, when I define the index like this, it will give a "Cannot use a scalar value as an array" error:
foreach($tools as $i => $val) { Why? And what is the best way to handle this, please? Edited April 11, 2020 by StevenOliverHey i want to make category pages with php. so i have embeded youtube videos on my website and i want to make a category so that every embeded link that is named under that category goes on the new web page i am making. so lets say i have a category in my table that is called "Call of duty black ops" would the code be something like this ? ( and yes i want it to be ordered by date aswell ) Code: [Select] $query = "SELECT * FROM `G4V_Videos`.`Category`.`Call of duty black ops` ORDER BY `Date added` DESC LIMIT 24"; $result = mysql_query($query); thanks guys! i have to pass a variable to so many pages i mean variable post from index.html to page1.php and it has to pass to other pages like page2.php, page3.php and so on, i tried session_start() but i am getting the below error so please tell me how it is possible to Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by and Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent Hi, I want to create a simple online buying site. I have an admin site with login where admin can add items to the database table, and change other fields such as price and description of product and I want to make a public html page to display the updates made in my admin page. I appreciate it if someone give me some suggestions on how to get started. My admin php scripts is working fine (so SQL queries for UPDATE, DELETE, INSERT) but I don't know how to 'sync' it with the an nice html table of products the user can checkbox off to purchase on the public website. Let me know, thanks. I been having this small curiosity "how to" for some time hope any one could give me an orientation. I m doing an website (from scratch as a self learning project) to "host" a list of movies and at the moment i m using the $_GET variables as navigation indicators (for example http://server/?go=settings, page will display the user settings) and the $_POST variables to pass all kind of form related data. At the moment i m done with all the authentication part (PHP,MySQL + cookies), but what m missing is a way to tell the user that "well your username or password is wrong", sounds silly i know ... i looked an fancy way of sending an user the warning and i found this small project of jquery called pnotify, where a small ballon will show on the page an message. My problem is to make the ballon apear upon an error of the authentication (if i just paste the code into the body it works). Besides the $_POST or $_GET is there anyway to pass an error code to an object? This is the code i have for the menu that decides all page navigation: page.php Code: [Select] <?php session_start(); $mysql = new mysql($dbhost,$dbuser,$dbpwd,$dbname,$seckey); $auth = new authentication(); require_once(INCLUDES_PATH.'page.header.php'); require_once(INCLUDES_PATH.'inc.actions.php'); require_once(INCLUDES_PATH.'page.footer.php'); action.php Code: [Select] <?php $do = isset($_GET["do"])?$_GET["do"]:""; switch ($do) { case "login": if( isset( $_POST['usr'] ) && isset( $_POST['pwd'] ) ){ $login = $auth->login( $_POST['usr'], $_POST['pwd'], $mysql); header( "Location:$_PATHS[home]" ); } else{ require_once(INCLUDES_PATH.'page.login.php'); } break; case "logout": $auth->logout(); header("Location:$_PATHS[home]"); break; default: require_once(INCLUDES_PATH.'page.body.php'); break; } ?> Thank you in advance. Hii,
I created a view with few selected data from table. I provided link to title to have redirection on editing page. I am passing variables to dynamic editing page. In editing page, I created a form which is filled automatically using id. Now on submission of updated form I redirected it to another php page for back-end processing. But now I am not getting data of inputs from editing page. POST and GET both are not working. I don't understand why is it not passing values.
Login ► View Data ► Select Data To Update ► Update Is not working here
Edited by Parth_Innovator, 05 December 2014 - 12:57 PM. I'm talking like /uploads at the main folder where index.php or index.html goes
These photos theoretically are meant to be publicly viewed anyway so what is bad about that?
Let's say profile pictures.
I mean couldn't anyone scrape facebook and "steal" profile pictures? I don't know why but I'm just wondering.
How can I make my PHP script non accessible to users? I am wanting to cron the script and don't want anybody to spam the script as it would cause performance issues if it was constantly spammed by a user. Can I put the PHP file anywhere that only the server can run it?
Thanks any info would be awesome.
I'm storing website files online and each user can upload their own files and admin can upload files for that user specifically. How would I go about making sure nobody else can download their PDF file? Would it be a case of assigning a folder for each user's documents and not allowing access to any other user to that folder?
Thanks in advance.
Hi guys, I hope you may be able to shed some light on a problem I am having. I am fairly new to PHP although do understand bits and peices such as login system. Basically - I have a website with lets say 10 users (more like 500 but that will do for now!) - Each user has their own page wich is password protected. Each month - I want to be able to upload PDF files to the server - which CAN ONLY BE ACCESSED BY THE RELEVANT user. They must not be able to see each others PDF files. To do this I have been advised to have a non-web accessible folder on my server to put the PDF's in and then use PHP to handle the operation. Therefore my path would be: 1/ User logs into page 2/ User clicks the PDF link 3/ PF link goes to PHP page that checks they are logged in and then the PDF they want before delivering to the server. I do actually understand the theory but my PHP is not at the stage where I can just write the code that will handle the operation. I am also very confused over how to access the non-web accessible files! Could anyone please give me an example of the code I will need to use to a) check the user is logged in (I guess I can use the same code I used for the login) and then b) call the relevant PDF and display it? I have been given a path of c:\blahblahblah to access my PDF files but don't even know how to begin implimenting this! Here is the code I use for my login system. Could anyone show me how to adapt it to get what I need? Thank so much for anyone that can help - hopefully I will be in a position to give back one day! -------------------------------------------- if(isset($_SESSION['loggedin'])) { header("Location:" . strtolower($username) . ".php"); if(isset($_POST['submit'])) { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $mysql = mysql_query("SELECT * FROM mydb WHERE username = '{$username}' AND password = '{$password}'"); if(mysql_num_rows($mysql) < 1) { die("Password or Username incorrect! Please <a href='login.php'>click here</a> to try again"); } $_SESSION['loggedin'] = "YES"; $_SESSION['username'] = $username; header("Location:" . strtolower($username) . ".php"); AND THEN IN THE HEADER OF THE PAGES <?php session_start(); if(!isset($_SESSION['loggedin'])) { header('Location: /login.php'); } elseif ($_SESSION['username'] . '.php' != basename($_SERVER['SCRIPT_FILENAME']) ) { // Logged in user attempting to view someone else's page header("Location:" . strtolower($_SESSION['username']) . ".php"); exit; } ?> Again - any help would be truly appreciated. I will say now that the last person I asked said "Use the open() function - that'll work!" ...... answers like that are a bit lost on me at the moment and leave me even more confused! Cheers in advance Hello. I want to create some variables dynamically. I have this so far: for($i = 1; $i <= 4 ; $i++){ $a = 'member' . $i; $$a = array(); } Does that give me this 4 arrays?: $member1, $member2, $member3 and $member4? Of course I want some process inside of the loop, but that would depend on the variables. Would that work? thanks! Hi All, For the last post,I m very happy that i found out so many great mind who are watching my problem and solving online. anyway great thanks for all, just now i m making a project and i m facing problem in the below code "i need to know how can make this code easy or how can i make rdata variable accessible for the download button" generatecv.php file <?php if(isset($_REQUEST['submit'])) { $name=$_REQUEST['pname']; $pin=$_REQUEST['pin']; $mnumb=$_REQUEST['mnumber']; $tnumb=$_REQUEST['telenumber']; $email=$_REQUEST['email']; $obj=$_REQUEST['obj']; $title=$_REQUEST['jobt']; $activity=$_REQUEST['activity']; //$file=fopen('test.doc','a+'); //echo @fwrite($file,$name); //fclose($file); $rdata=""; $rdata.="<table id='cvtbl' border='0' align='center' > <th>Resume</th> <tr><td>".$name.":</td></tr><tr><td>Objective:</td></tr>"; $rdata.="<tr><td>".$obj."</td></tr>"; $rdata.="<tr><td>Email:".$email."</td></tr>"; $rdata.="</table>"; echo $rdata; echo "<style>#cv{display:none;}</style>"; echo "<form><input type='submit' name='dwnld' id='dwnld' value='Download'/></form>"; } if(isset($_REQUEST['dwnld'])) { header("Content-type:application/msword"); header("Content-disposition:attachment;filename=test.doc"); $fp=fopen("test.doc","r") or die("try again"); $data=fread($fp,filesize('test.doc')); fwrite($fp,$rdata); echo $data; fclose($fp); //echo $rdata."<br />".$rdata; } ?> this is the html file <html> <title>User details</title> <body> <form name='cv' id='cv' method="post" enctype="multipart/form-data" action='generatecv.php'> Enter your name<input type=text name=pname id=pname /> Zip Code<input type=text name=pin id=pin /> Mobile number<input type=text name=mnumber id=mnumber /> <br />Telephone number <input type=text name=telenumber id=telenumber /> Email address<input type=text name=email id=email /> <br />Enter your objective<br /><textarea name=obj id=obj cols=30 rows=5 ></textarea> <!--University Name <input type=text name=university id=university /> --> <br /><!--uni, city ,state ,type of degree ,full degree name,date--> <br /><!--Experience<br /><textarea name=exp id=exp cols=30 rows=5 ></textarea> --> Select type of resume <select name='type' id='type' onChange=''> <option value='0'>Graduate</option> <option value='1'>Post Graduate</option> <option value='2'>Non Graguate</option> <option value='3'>Other</option> </select> Job title<input type='text' name='jobt' id='jobt' /> <br /><!--company name,city ,state ,office number ,job description ,date of employment--> <br />Activity <input type=text name=activity id=activity size=100 /> <br />Honors/Awards<input type=text name=honor id=honor size=100 /> <input type='submit' name='submit' id='submit' value='Generate CV' /> <input type='reset' name='Reset' id='submit' value='Reset' /> </form> </body> </html> Many thanks in advance I'm creating a website where writers can share stories. I have a section where the users enter a summary of there story. Anyways in the area where the story in being listed I want the the title of the story(with a link to the story) underneath the link I would like a summary. BUUUUT, the summary can get pretty long(up to 900 chars). So I am wondering if I can shorten the variable where the summary is stored, I want it to be shorten to a fixed amount of characters(maybe 200?) then followed by a ... . So how can I do this? Hello, I'm working on a script which should read lines from a file keyword.txt create a new file with that variable name, insert the content close and loop through all the lines in the keyword file. The loop itself works but now that i'm adding the fopen fwrite and fclose i'm running into trouble. I'm not that great a coder and have pieced this together so far. I'd appreciate any suggestions on how to fix the fopen and other file functions to create unique files using the keywords from the file Thanks in advance <?php $read_file = file('keywords.txt'); foreach($read_file as $var) { $file = fopen($var, "x"); $content = "<!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=utf-8\" /> <title>This is the title</title> </head> <body> <h2>The keyword is $var</h2> <p><p> </body> </html>"; fwrite($file, $content); fclose($file); } ?> I am trying to allow the user to update a variable he chooses by radio buttons, which they will then input text into a box, and submit, to change some attributes. I really need some help here. It works just fine until I add the second layer of variables on top of it, and I can't find the answer to this question anywhere. <?PHP require('connect.php'); ?> <form action ='' method='post'> <select name="id"> <?php $extract = mysql_query("SELECT * FROM cars"); while($row=mysql_fetch_assoc($extract)){ $id = $row['id']; $make= $row['make']; $model= $row['model']; $year= $row['year']; $color= $row['color']; echo "<option value=$id>$color $year $make $model</option> ";}?> </select> Which attribute would you like to change?<br /> <input type="radio" name="getchanged" value="make"/>Make<br /> <input type="radio" name="getchanged" value="model"/>Model<br /> <input type="radio" name="getchanged" value="year" />Year<br /> <input type="radio" name="getchanged" value="color" />Color<br /><br /> <br /><input type='text' value='' name='tochange'> <input type='submit' value='Change' name='submit'> </form> //This is where I need help... <?PHP if(isset($_POST['submit'])&&($_POST['tochange'])){ mysql_query(" UPDATE cars SET '$_POST[getchanged]'='$_POST[tochange]' where id = '$_POST[id]' ");}?> Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment. My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted." The program should not accept any numbers greater than 100 or any characters. Once I do this, I must take a second number and do a similar thing. Finally, I must have a statement show up at the bottom stating which number is greater. Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters. Any help you can provide will be greatly appreciated! I have just re-installed Xampp and suddenly my sites are now displaying lots of: Notice: Use of undefined constant name - assumed 'name' in ... Notice: Use of undefined constant price - assumed 'price' in ... this is an example of the line its refering too: $defineProducts[1001] = array(name=>'This is a product', price=>123); |