Jump to content

Mudsaf

Members
  • Posts

    462
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by Mudsaf

  1. So basically i have this code below on test file which works perfectly.

    header('Content-Type:'.'image/jpeg');
    header('Content-Length: ' . filesize('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg'));
    readfile('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg');

    But when i try to implement it on my OOP file, i assume the Content-Type gets changed automatically to html/text? Any ideas what might cause this?

    <?php
    /* Grab image file outside of webroot directory */
    if (session_status() == PHP_SESSION_NONE) {
    session_start();
    }
    
    class LoadImage {
    	private $GFilePath;
    	private $DPath = "local";
    	
    	//Fetch single image by ID
    	public function fetchIByID($iid) {
    		
          	//bit validation
    		if (!is_numeric($iid)) {
    		die();
    		}
    		
    		require_once("main_db_con.php"); //DB connection file => might cause issue ?
    		$conn = new DbCon;
    		$con = $conn->connect();
    		
    		
    		
    		//Get image by id parameter
    		if ($res_gibi = $con->prepare("SELECT `image_url`, `mime_type` FROM images WHERE id = ? AND status = ? LIMIT 1")) {
    		$prog_req1 = 1; //Unpublished imagec
    		$res_gibi->bind_param("ii",$iid,$prog_req1);
    			if ($res_gibi->execute()) {
    				$res_gibi->store_result();
    				
    				if ($res_gibi->num_rows == 1) {
    					
    				$res_gibi->bind_result($iurl,$im_type);
    				if ($res_gibi->fetch()) {
    						if ($istatus == 1) {
    						
    						
    						$this->GFilePath = "E:/xampp/uploads/gallery/files/"; //local path
    						
    							
    							
    							
    							if (file_exists($this->GFilePath.$iurl)) {
    							
                                //below not returning image
    							header('Content-Type:'.'image/jpeg');
    							header('Content-Length: ' . filesize('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg'));
    							readfile('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg');
    
    							//header('Content-Type:'.$im_type);
    							//header('Content-Length: ' . filesize($this->GFilePath.$iurl));
    							//readfile($this->GFilePath.$iurl);
    							
    							//$this->GFilePath.$iurl . " | " . $im_type;
    					
    							
    							//$ftype;
    							//"<br>" . $fpath;
    							
    							} else {
    							
    							}
    						
    						} else {
    						// "<p>Unauthorized or image does not exist.</p>";
    						}
                      
    					} else {
    					//
    					}
    				} else {
    				//err_fetch	
    				}
    			} else {
    				// "x";
    			//ERR_EXEC	
    			}
    		} else {
    		//ERR_PREP	
    		// "y";
    		}
    		//Closes SQL connection
    		unset($conn);
    		$con->close();
    	}
    	
    	
    	//public function __construct() {
    	//$this->fetchIByID(994);	
    	//}
    }
    
    
    if (isset($_GET['image']) && is_numeric($_GET['image'])){
    $iload = new LoadImage;
    $iload->fetchIByID($_GET['image']);
    }
    ?>

    Not fully finished code, slightly altered but you get the idea + big ugly code atm but have to fix lated.

    Looks like attached file on browser.

     

    cap2.PNG

  2. Yo, back with OOP related question. So if i have database connection with OOP style (example below)

    class MyDB {
    	protected $con;
    	private $db_host = "x"; //Database host
    	private $db_user = "x"; //Database username
    	private $db_pass = "x"; //Database user password
    	private $db_name = "x"; //Database name
    	
    	public function connect() {
    	$this->con = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
    		if ($this->con->connect_error) {
    		die("Connection error, please try again later.");
    		} else {
    		return $this->con;
    		}
    	}
    }

    Is there way to prevent var_dump showing the database connection information if linked. (example below)

    $mydb = new MyDB;
    $con = $mydb->connect();
    
    var_dump($con);

    ^Example like this, ofc I wouldn't dump the connection info itself, but if its linked in other class i think it shows it anyways when doing the var_dump.

  3. Got any tips for including files with OOP system? At the moment i can do like this.

    include_once "inc/ps.db_con.php";
    include_once "inc/script.myinformation.php";

    But i assume i should do ps.db_con.php call inside script.myinformation.php instead, since it always requires it. (Like below)

    include_once "inc/ps.db_con.php";
    class MyInformation extends ConnectDB {

    This works if i'm in index.php file, but if i want to go subfolder it stops working. I could use "/" path, but i've always seen it as inconvenient way. Example below.

    include_once "/myproject/inc/ps.db_con.php";
    class MyInformation extends ConnectDB {

     

     

  4. Encountered another problem, i edited my code alot and not sure how to access the data properly.

    var_dump($object->fetchUD("testuser"));

    This returns all data of the user, but lets say i need to access the testuser email, tried below.

    //var_dump($object->fetchUD("testuser")->email);

    Basically my class has protected properties predefined (like below)

    class AccountInformation extends ConnectDB {
    	protected $username;

    Then i try to run public function with variable ($un), which is the username from fetchUD "testuser".

    public function fetchUD($un) {

    and at the end i got, which gives me my result. I'm wondering how can i handle the data separately just like i tried to do on the second part. "$object->fetchUD("testuser")->email"

    return $this;

    This is what i get out (which continues to all DB info etc)

    object(AccountInformation)#1 (13) { ["username":protected]=> string(8) "testuser" ["uid":protected]=> int(1) ["email":protected]=> string

    --------------------

    2nd thing, is there way to prevent var_dump from displaying the database information (which is private) from other class i made?

    <?php
    class ConnectDB {
    	private $host;
    	private $user;
    	private $pass;
    	private $db;
    	
    	protected function conDB() {
    		$this->host = "";
    		$this->user = "";
    		$this->pass = "";
    		$this->db = "";
    		
    		/* Makes the Database connection */
    		$con = new mysqli($this->host,$this->user,$this->pass,$this->db);
    		return $con;
    	}
    }
    ?>

    Think i got how the system works now with private, protected and public. If im correct i can only use public variablesfor example

    public $username

    which then i can access via this 

    $object->fetchUD("testuser")->username

     

  5. So currently learning a bit OOP by myself. Made this script with little knowledge i have about OOP.  I tried changing public function __construct() to private/secure, but it gave me error and struggled to find real solutions how to do it as secured or private.

    <?php
    session_start();
    
    class AccountInformation {
    	public function __construct() {
    		//Fetch account information, prevent premission for start
    		
    		if (isset($_SESSION['username'])) {
    		
    		if (!isset($con)) {
    		include "inc/con.php";	
    		}
    		
    			if ($res_fai = $con->prepare("SELECT firstname FROM tb_accounts WHERE username = ? LIMIT 1")) {
    			$res_fai->bind_param("s", $_SESSION['username']);
    			
    				if ($res_fai->execute()) {
    				$res_fai->bind_result($acc_fname);
    				$res_fai->fetch();
    				
    				$this->fname = $acc_fname;
    				}
    			}
    		
    		} else {
    			$this->fname = null;
    		}
    	}
    	
    }
    
    $acc_data = new AccountInformation;
    
    //echo $acc_data->fname;
    //var_dump($acc_data);
    ?>

    So echo and dump works perfectly fine, but i'm wondering if theres anything i could do better? Security/Management-wise. My own script is slightly different, but this runs the same idea that i have (just getting more data from db). 

    I also tried to to set fname like this below, but got error (assuming i cant set private "variables" inside public function

    private $fname = $acc_fname;

     

  6. Have you checked if this returns the unid() value?

    echo "line 106 &nbsp" . $test . "<br/>";

    Just double check if everything goes through. Simplified your code for testing and worked for me.

    <?php
    function Send_mail($to){
    	return uniqid();	
    }
    $test = Send_mail("1");
    	if (isset($_POST['code']) && !empty($_POST['code'])){
            $code = htmlspecialchars ($_POST['code']);
    	}
    // I don't get echo good, because $test is empty.
    	if (!empty($code)){
    		if ($code == $test){
    			echo "good";
    		}
    		else{
    			echo "bad";
    			echo "<br>";
    			echo "line 149 &nbsp" . $code;
    			echo "<br>";
    			echo "line 151 &nbsp" . $test;
    		}
        }
    	if (!empty($test)){
    		echo "line 156 &nbsp" . $test;
    		?>
    			<div class="code">
    				<form method="post">
    					Code: <input type="text" name="code">
    					<input type="submit">
    				</form>
    			</div>
    			
    		<?php
    	}
     //END for loop
    ?>

    Also noticed that what you're trying to do is not possible with method you're currently doing. Every time you do post/get action it will get new unique id and wont match the form code (unless you guess the uniqid). Instead store the uniqid to php session or database to make it work and simply remove it after.

  7. On 3/14/2019 at 12:15 AM, SimonB said:

    Not sure I understand - I have tried entering a font size (16pt) just about everywhere on the php form to no effect

    for instance, where do I  insert font size in this phrase

    function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br />

    <br />";
            die();

     

    I have tried it as a head, and at the top of the document, and also before each line of text (and different font sizes) to no discernable effect

     

    And here you are using HTML <br> tags.

    More about making styles @https://www.w3schools.com/css/default.asp

  8. On 3/14/2019 at 10:20 AM, SimonB said:

    what is " echo all of the HTML tags, "

     

    The code I have is cut and pasted from a "this is how to do a contact form" I don't know what any of it does.

    Heres example below, if you didn't understand the above one.

    <html>
    <head>
    <style>
    h2 { font-size:28px; }
    p { font-size:20px; }
    </style>
    </head>
    <body>
    <?php
    //Comment
    //So you echo out HTML code with tags (example below)
    echo "<h2>This is heading 2</h2>"; //Heading 2 --- h2 tags
    echo "<p>Text inside paragraph</p>"; //Paragraph tags
    echo "No HTML whatsoever"; //plain text
    ?>
    </body>
    </html>

     

     

  9. Hello, i'm trying to create file upload box which is activated by both, file drag & drop and onclick. Currently drag & drop works fine for me, but i have no idea how to implement the feature for the click with automatically uploading the image when file area is changed.

     

    Heres my code so far

    <script>
    $(function() {
    	var dropzone = document.getElementById('upload_area');
    	
    	var upload = function(files) {
    		var formData = new FormData(),
    		xhr = new XMLHttpRequest(),
    		x;
    			
    		for (x = 0; x < files.length; x++) {
    			formData.append('file[]', files[x]);
    		}
    			
    		
    		xhr.onload = function() {
    			var data = this.responseText;
    			console.log(data);
    		}
    		
    		xhr.open('post','script/php/iupload.php?fid=gallery');
    		xhr.send(formData);
    	}
    	
    	
    	dropzone.ondrop = function(e) {
    		e.preventDefault();
    		this.className = 'dropzone';
    		upload(e.dataTransfer.files);
    	}
    		
    	dropzone.ondragover = function() {
    		this.className  = 'dropzone dragover';
    		return false;
    	}
    	
    	dropzone.ondragleave = function() {
    		this.className = 'dropzone';
    		return false;
    	}
    	
    	
    });
    
    </script>
    
    <div id='upload_area' class='dropzone'>Drag & Drop or click to upload files</div>
    

    I was able to try dropzone.onclick event, but didn't get anything to work without $("#upload_area").submit(); function. Any tips guides / tutorial videos are welcome, since im trying to improve as developer.

  10. Hello, i'm currently facing problem where i have 2 tables like this. I have no clue how to make this happen, would appreciate any help example links for guide or solutions.

     

    //First

    -------

    id | name | stuff

    1 - Test - bla //Example values

    2 - Test2 - Bla2

    -------

    //Second

    -------

    id | related | rating

    1 - 2 - 5 //Example values

    2 - 2 - 1

    3 - 2 - 2

     

    So i'm trying to do SQL query where i can select * data from first table, and join on second table with AVG(rating) where related (second table) = id(first table).

  11. Hello, i'm wondering why my JavaScript / jQuery code doesn't work.

    $(function() {var i=0;$("#preimgarea").prepend("<center>");bg_imglist.forEach(function(entry) {$("#preimgarea").append("<img style='someCSS' class='setBG' id='" + i + "' src='" + entry[0] + "'>");i++;if (i == bg_imglist.length) {$("#preimgarea").append("</center>");}});	});

    Basically the code pastes <center></center> then the forEach data.

  12. if (!file_exists("img/" . $_GET['remove'])) {echo "<script>console.log('Prelog');$(function() {	$('#message_area').html('The file you try to remove does not exist.').fadeIn(500, function() {		setTimeout(function () {		console.log('Log works');			window.open('?manage=true', '_self');    		}, 2000);	});});</script>";}

    Fixed, forgot there is small script on footer.php that messed up my code. Feel like idiot now T_T

  13. Hello, i'm wondering what could be the reason that my jQuery wont execute while echoing, but manual execution on browser console works.

     

    Code in PHP (This code will still open the window on manage=true, no errors on console)

    echo "<script>$(function() { $('#message_area').html('The file you try to remove does not exist.').fadeIn(500, function() {  setTimeout(function () {  window.open('?manage=true', '_self'); <!-- This executes, this comment is not on the code -->  }, 2000); });});</script>";

    Message area html below.

    <div id="message_area"><strong>Note</strong>:Enable JavaScript for more advanced website surfing.</div> 
×
×
  • Create New...